我有一个表示代码字节的整数列表.如何快速,高效地将它们写入二进制文件.
我试过了:
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分钟).
解决方法:
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() type,输入相同;毕竟,你不是在创造之后改变价值观.
标签:python,performance,list,file
来源: https://codeday.me/bug/20190725/1532913.html