簡體   English   中英

讀取記錄數據的二進制文件並使用int(python)輸出到新文件

[英]Read binary file of logging data and output to new file with int (python)

我一直致力於一個嵌入式軟件項目,該項目使用FATFS模塊將傳感器數據寫入SD卡。 數據的數據類型為uint32_t(4個字節),輸出為二進制文件。

我正在嘗試編寫一個python腳本來讀取二進制文件(並將數據解析為int並寫入新文件)。 我目前的代碼,

def read():
with open("INPUT1.TXT", "rb") as binary_file:
    # Read the whole file at once
    data = binary_file.read()
    print(data)

這給了我一個十六進制的價值,

    b'    \x01   \x02   \x03   \x04   \x05   \x06   \x07   \x08   \t   \n   \x0b   \
x0c   \r   \x0e   \x0f   \x10   \x11   \x12   \x13   \x14   \x15   \x16   \x17
 \x18   \x19   \x1a   \x1b   \x1c   \x1d   \x1e   \x1f       \x01   \x02   \x03
  \x04   \x05   \x06   \x07   \x08   \t   \n   \x0b   \x0c   \r   \x0e   \x0f
\x10   \x11   \x12   \x13   \x14   \x15   \x16   \x17   \x18   \x19   \x1a   \x1
b   \x1c   \x1d   \x1e   \x1f      '

打印每4個字節時,甚至會丟失一些數字,

f = open("INPUT2.TXT", "rb")
try:
    bytes_read = f.read(4)
    while bytes_read:
        print(bytes_read)
        bytes_read = f.read(4)
finally:
    f.close()

給出結果

b'    '       #supposed to be \x00
b'\x01   '
b'\x02   '
b'\x03   '
b'\x04   '
b'\x05   '
b'\x06   '
b'\x07   '
b'\x08   '
b'\t   '      #supposed to be \x09
b'\n   '      #supposed to be \x0a
b'\x0b   '
b'\x0c   '
b'\r   '      #supposed to be \x0d
b'\x0e   '
b'\x0f   '
b'\x10   '
b'\x11   '
b'\x12   '
b'\x13   '
b'\x14   '
b'\x15   '
b'\x16   '
b'\x17   '
b'\x18   '
b'\x19   '
b'\x1a   '
b'\x1b   '
b'\x1c   '
b'\x1d   '
b'\x1e   '
b'\x1f   '

但是當我在十六進制編輯器中讀取二進制文件時,所有二進制文件看起來都是正確的?!

如果我想一次讀取4個字節,並寫入新文件(類型為int),我該如何實現呢?

謝謝,

亨利

nums = []
with open("INPUT2.TXT", "rb") as file:
    while byte:
        byte = file.read(4)
        nums.append(int.from_bytes(byte, byteorder="little"))

這應該為python 3做。

看起來您的字節是從您的示例中翻轉的,因此我將字節順序更改為很少。 如果它們沒有被翻轉,那么將它改回大。

另一個奇怪的事情:它看起來像0x00變成b“”,而不是b“\\ x00”。 如果是這樣的話,那就改為:

nums = []
with open("INPUT2.TXT", "rb") as file:
    while byte:
        byte = file.read(4)
        nums.append(int.from_bytes(byte.replace(b" ", b"\x00"), byteorder="little"))

以下是您提供的示例。

>>> test = [b'    ',
b'\x01   ',
b'\x02   ',
b'\x03   ',
b'\x04   ',
b'\x05   ',
b'\x06   ',
b'\x07   ',
b'\x08   ',
b'\t   ',
b'\n   ',
b'\x0b   ',
b'\x0c   ',
b'\r   ',
b'\x0e   ',
b'\x0f   ',
b'\x10   ',
b'\x11   ',
b'\x12   ',
b'\x13   ',
b'\x14   ',
b'\x15   ',
b'\x16   ',
b'\x17   ',
b'\x18   ',
b'\x19   ',
b'\x1a   ',
b'\x1b   ',
b'\x1c   ',
b'\x1d   ',
b'\x1e   ',
b'\x1f   ']

>>> for t in test:
>>>     print(int.from_bytes(t.replace(b" ", b"\x00"),  byteorder="little"))
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

你也許可以這樣做

for i in range(0, len(data), 4)
    d = struct.unpack('I', data[i:i+4])
    print(d)

如果只是將uint32_t數字打包成二進制文件,我想你可以在文件上使用read()函數

num_list = []
with open("INPUT1.TXT", "rb") as binary_file:
    byte_data = 0x1 # Initial placeholder for the loop
    while byte_data:  
        byte_data = binary_file.read(4) # 4 being the number of bytes to read at a time
        num_list.append(int(byte_data))
#  Do something with num_list

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM