从一个模拟工具中,我得到了一个包含一些测量点的二进制文件。我需要做的是:解析度量值并将它们存储在列表中。你知道吗
根据该工具的文档,文件的数据结构如下所示:
First 16 bytes are always the same:
Bytes 0 - 7 char[8] Header
Byte 8 u. char Version
Byte 9 u. char Byte-order (0 for little endian)
Bytes 10 - 11 u. short Record size
Bytes 12 - 15 char[4] Reserved
The quantities are following: (for example one double and one float):
Bytes 16 - 23 double Value of quantity one
Bytes 24 - 27 float Value of quantity two
Bytes 28 - 35 double Next value of quantity one
Bytes 36 - 39 float Next value of quantity two
我也知道,编码是小端的。你知道吗
在我的用例中有两个数量,但它们都是浮动的。你知道吗
到目前为止,我的代码如下所示:
def parse(self, filePath):
infoFilePath = filePath+ '.info'
quantityList = self.getQuantityList(infoFilePath)
blockSize = 0
for quantity in quantityList:
blockSize += quantity.bytes
with open(filePath, 'r') as ergFile:
# read the first 16 bytes, as they are not needed now
ergFile.read(16)
# now read the rest of the file block wise
block = ergFile.read(blockSize)
while len(block) == blockSize:
for q in quantityList:
q.values.append(np.fromstring(block[:q.bytes], q.dataType)[0])
block = block[q.bytes:]
block = ergFile.read(blockSize)
return quantityList
QuantityList来自上一个函数,包含quantity结构。每个数量都有一个名称、数据类型、称为bytes的lenOfBytes和一个准备好的值列表values。你知道吗
因此在我的用例中,有两个数量:
dataType = "<f"
bytes = 4
values=[]
解析函数完成后,我用matplotlib
绘制第一个数量。从所附的图片中可以看出,解析过程中出现了一些错误。你知道吗
但是我找不到我的错。你知道吗
今天早上我解决了我的问题。你知道吗
解决办法再简单不过了。你知道吗
我变了
收件人:
注意从'r'到'rb'的变化。你知道吗
python文档让我明白了:
因此,最终解析的值如下所示:
Final values
相关问题 更多 >
编程相关推荐