python用input输入整数变成二进制,如何在python中将整数列表写入二进制文件

I have a list of integers which represent bytes of code. How can I write them to a binary file fast and more efficiently.

I have tried:

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())

and

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))

both get me a result which I think is correct (I have yet to reverse the process) but both take a long time (6min and 4min respectfully).

解决方案

Use a bytearray() object, write that straight to the output file:

with open (output1, "wb") as compdata:

for row in range(height):

bytes = bytearray(cobs(delta_rows[row].getByte_List()))

compdata.write(bytes)

A sequence of integers is interpreted by a bytearray() as a sequence of byte values.

In Python 3, you can use a bytes() type as well, with the same input; you are not mutating the values after creation, after all.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值