代码之家  ›  专栏  ›  技术社区  ›  shjnlee

读取日志数据的二进制文件,并用int(python)输出到新文件

  •  1
  • shjnlee  · 技术社区  · 8 年前

    我一直在从事一个嵌入式软件项目,该项目使用FATFS模块将传感器数据写入SD卡。数据的数据类型为uint32\u t(4字节),输出为二进制文件。

    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      '
    

    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   '
    

    谢谢

    亨利

    3 回复  |  直到 8 年前
        1
  •  3
  •   jacoblaw    8 年前
    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
    
        2
  •  0
  •   Gribouillis    8 年前

    你也许可以用

    for i in range(0, len(data), 4)
        d = struct.unpack('I', data[i:i+4])
        print(d)
    
        3
  •  0
  •   shiv    8 年前

    如果它只是打包到二进制文件中的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