簡體   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