Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按字节读取二进制数据并使用python提取数据_Python_Python 2.7_File_Binary_Hex - Fatal编程技术网

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

按字节读取二进制数据并使用python提取数据,python,python-2.7,file,binary,hex,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: ...........

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

 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字节的位置十六进制数据,并将其转换为浮点,将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个字段。

这是一种简单的方法,可以提取分隔符之间的数据,并从文件中每个消息的前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 

感谢您的回复。我将仔细检查您的解决方案并进行检查。它会给出错误“unpack需要长度为12的字符串参数”。唯一的方法
数据[:12]
是if
len(data)<12
,在这种情况下,它不能包含浮点和双精度。你的问题中是否缺少一些信息,或者这只是你需要处理的文件中的一个错误?你能告诉我12在这里是什么意思吗。我尝试了不同的方法,能够提取字节位置5到8的数据,但不能提取整个文件的数据。请查看我的帖子。我又编辑了一次