简体   繁体   English

读取记录数据的二进制文件并使用int(python)输出到新文件

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

I've been working on an embedded software project that writes sensor data to SD card using FATFS module. 我一直致力于一个嵌入式软件项目,该项目使用FATFS模块将传感器数据写入SD卡。 The datatype of the data is uint32_t (4 bytes) and the output is binary file. 数据的数据类型为uint32_t(4个字节),输出为二进制文件。

I'm trying to write a python script to read the binary file (and parse the data to int and write to a new file). 我正在尝试编写一个python脚本来读取二进制文件(并将数据解析为int并写入新文件)。 My current code, 我目前的代码,

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

and that gives me a chunk of value in hex, 这给了我一个十六进制的价值,

    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      '

When printing each 4 bytes, some numbers are even missing, 打印每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()

give result of 给出结果

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   '

But when I read the binary file in a hex editor, all the binary appears to be correct?! 但是当我在十六进制编辑器中读取二进制文件时,所有二进制文件看起来都是正确的?!

If I want to read 4 bytes at a time, and write to a new file (in type int), how could I achieve it? 如果我想一次读取4个字节,并写入新文件(类型为int),我该如何实现呢?

Thanks, 谢谢,

Henry 亨利

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

This should do it for python 3. 这应该为python 3做。

It looks like your bytes are flipped from your example, so I changed byte order to little. 看起来您的字节是从您的示例中翻转的,因此我将字节顺序更改为很少。 if they aren't flipped, then change it back to big. 如果它们没有被翻转,那么将它改回大。

Another weird thing: it looks like 0x00 is getting turned into b" ", instead of b"\\x00". 另一个奇怪的事情:它看起来像0x00变成b“”,而不是b“\\ x00”。 if that's the case, then do this instead: 如果是这样的话,那就改为:

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

Here's an example with what you provided. 以下是您提供的示例。

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

You could perhaps do it with 你也许可以这样做

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

If it's just uint32_t numbers packed into a binary file, I think you can use the read() function on the file 如果只是将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