【发布时间】:2018-12-04 18:34:27
【问题描述】:
我有一个包含以下内容的文件:
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)
【问题讨论】:
标签: python python-2.7 file binary hex