按字节读取二进制数据并使用python提取数据

时间:2018-06-26 06:20:58

标签: python python-2.7 file binary hex

我有一个文件,该文件包含以下内容:

 0:   10 51 03 37 7F 43 82 99  45 3F E7 35 3A A8 80 B9   .text ? etc  # text 
10:   3D 3F F4 49 F8 9A 7A 85  03 40 8A C8 46 DE 0A 1A   . -@ =! ETC  # text 
30:   .......................................................
40:   ...........................................10 03

     Repeat next instant 
     10 51 ................................................................
     ............................................ 10 03

这是一条消息,其中10和51表示特定消息的开始,而10 03表示消息的结束。 10表示0字节位置,51表示1字节位置并继续。

我的目的是读取5-8字节位置的十六进制数据,并在每个瞬间将其转换为float,将9-16位置字节转换为两倍

到目前为止,我的实现只读取前31个十六进制数据。

 import struct

with open("RawData.log",'rb') as fin:
    data1 = ["{:02x}".format(ord(c)) for c in fin.read()]
    data2=''.join(data1)

    #data=pd.DataFrame({'test':data1})
    header = "51"
    tail   = "03"


   # header_index = data2.index(header)
    header_index=[i for i, s in enumerate(data1) if header in s]
    footer_index = [i for i, s in enumerate(data1) if tail  in s]
    if header_index >= 0 and footer_index >= header_index:
       body = data2[10:18]
       print struct.unpack('!f',body.decode('hex'))[0]

       #261.197418213 only 1 output not iterating for whole file. Similarly how to extract 9 to 16 byte position data to double for entire file.

如何读取整个文件,并在每次找到消息标头(10和51)时仅从十六进制数据中提取这2个字段

1 个答案:

答案 0 :(得分:0)

这是一种在定界符之间提取数据并从文件中每条消息的前12个位置解压缩浮点和双精度的简单方法:

with open('RawData.log', 'rb') as f:  # read data in binary format
    bs = f.read()
prev = None
start = None
end = None
for i, b in enumerate(bs):
    if prev == b'\x10' and b == b'\x51':
        # match beginning of message
        start = i + 1 
    if prev == b'\x10' and b == b'\x03':
        # match end of message
        end = i - 1 
    if start and end:
        data = bs[start:end]
        fmt = '!fd'   # unpack a float and double, big-endian
        flt, dble = struct.unpack(fmt, data[:12])
        print flt, dble
        # reset delimiter positions
        start = None
        end = None
    prev = b