繁体   English   中英

如何在python中将整数列表写入二进制文件

[英]How to write a list of integers to a binary file in python

我有一个表示代码字节的整数列表。 如何快速,高效地将它们写入二进制文件。

我努力了:

with open (output1, "wb") as compdata:
    for row in range(height):
        for data in cobs(delta_rows[row].getByte_List()):
            output_stream.append(Bits(uint=data, length=8))
    compdata.write(output_stream.tobytes())

with open (output1, "wb") as compdata:
    for row in range(height):
        bytelist = cobs(delta_rows[row].getByte_List())
        for byte in bytelist:
            compdata.write(chr(byte))

两个都给我一个我认为是正确的结果(我还没有扭转过程),但都需要很长时间(6分钟和4分钟)。

使用bytearray()对象 ,将其直接写入输出文件:

with open (output1, "wb") as compdata:
    for row in range(height):
        bytes = bytearray(cobs(delta_rows[row].getByte_List()))
        compdata.write(bytes)

整数序列由bytearray()解释为字节值序列。

在Python 3中,您也可以使用bytes()类型 ,具有相同的输入; 毕竟,你不是在创造之后改变价值观。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM