我需要创建10,000个随机整数的文件进行测试。我将在Python和C中使用该文件,因此我无法将datatables示为字符串,因为我不希望C中的整数转换的额外开销。

在Python中,我可以使用struct.unpack将文件转换为整数,但是我不能使用write()方法将其写入用于C中的文件。

Python有什么方法可以将整数而非integers-as-strings编写到文件? I使用print(val, file=f)f.write(str(val)),但在这两种情况下,它都写了一个字符串。

这是我现在的位置:

file_root = "[ file root ]"

file_name = file_root + "Random_int64"

if os.path.exists(file_name):
    f = open(file_name, "wb")
    f.seek(0)

for _ in range(10000):
    val = random.randint(0, 10000)
    f.write(bytes(val))

f.close()
f = open(file_name, "rb")

wholefile = f.read()
struct.unpack(wholefile, I)

我的unpack格式字符串是错误的,所以我现在正在研究。我对struct.unpack并不那么熟悉。

分析解答

bytes(val),当valint时,会创建指定长度的bytes对象。如果您的随机数为12345,则您正在编写12345零,而不是数字。诀窍是打包然后写每个整数。

结构模块字节订单,大小和对齐方式部分中,"<"写入字节"little endian"(Intel/AMD使用的字节顺序)。下一个字符可以是"L"到Wirte 4字节未签名的长整数或"Q",以编写8个字节。 4对于您的字符范围来说,它很大,并且会产生一个较小的文件,但是如果您想在将来要更大的值,则8个更大的"future proof"。

假设您不需要随机数中的重复序列,则可以创建整数列表,洗牌,然后一一写入文件。确保打开一个二进制文件,以免完成编码。

有了更多的清理

import random
import struct

file_root = "testfile"
file_name = file_root + "Random_int64"

with open(file_name, "wb") as f:
    for _ in range(10000):
        f.write(struct.pack("<Q", random.randint(0, 10000)))

您也可以使用bytearraypackinto首先构建缓冲区并写一次。

import random
import struct

file_root = "testfile"
file_name = file_root + "Random_int64"

buf = bytearray(10000*8)
for offset in range(10000*8, 8):
    struct.pack_into(buf, "<Q", offset, random.randint(0, 10000))

with open(file_name, "wb") as f:
    f.write(buf)

而且,如果您不介意在标准库之外使用软件包,则Numpy具有经典

import numpy as np
np.random.randint(10000, size=10000).tofile("test.bin")

如果我们要押注性能,那就是我要去的地方。