简体   繁体   English

使用struct在python3.6中读取结构化二进制数据

[英]reading structured binary data in python3.6 using struct

I really tried to look for this doubt in many ways but maybe since I never worked with binary files before I don't have any idea what the keywords to search similar things to help me out. 我确实尝试过以多种方式寻找这种疑问,但也许是因为我从不使用二进制文件,直到我不知道搜索相似内容的关键字可以帮助我什么时候。 That's why I am asking here. 这就是为什么我在这里问。

So, I have a file: 所以,我有一个文件:

path = 'myPath/file.pd0'
in_file = open(path, "rb")
read_file = in_file.read()
type(read_file) 

when I try to check what is inside read_file I get: 当我尝试检查read_file中的内容时,我得到了:

b'\x7f\x7f\xcc\x05\x00\x0f$\x00`\x00\xa2\x00$\x02\xe6\x02\xa8\x03\xd0\x032\x04d\x04\x96\x04\xa6\x04\xe0\x04'

The type of read_file is bytes. read_file的类型为字节。 When I try to use struct since it is the function people suggest I get the following error: 当我尝试使用struct时,因为它是函数,人们建议我得到以下错误:

import struct
struct.unpack('hhl', read_file[0:30])

error: unpack requires a buffer of 16 bytes

No matter what fmt I get unpack requires a buffer of n bytes. 无论我得到什么解压缩,都需要一个n字节的缓冲区。

The file structure that I am trying to read is defined as follow: 我尝试读取的文件结构定义如下:

HEADER (6 BYTES + [2 x No. OF DATA TYPES]) 标题(6个字节+ [2 x数据类型数])

FIXED LEADER DATA (60 BYTES) 固定的领先者数据(60字节)

VARIABLE LEADER DATA (66 BYTES) 可变领导者数据(66个字节)

CORRELATION MAGNITUDE (2 BYTES + 4 BYTES PER DEPTH CELL) 相关幅度(每个深度细胞2个字节+ 4个字节)

Any idea how I could start reading these bytes using struct or something similar in python? 知道如何使用struct或类似python的方法开始读取这些字节吗?

Thank you 谢谢

unpack() expects bytes which are the exact length of the format described by its first argument. unpack()期望字节是其第一个参数描述的格式的确切长度。 The format string 'hhl' describes data of 16 bytes (on your machine - see below), so you must pass a byte string of 16. If you want to parse only part of the bytes, you can do this: 格式字符串'hhl'描述了16个字节的数据(在您的计算机上-参见下文),因此您必须传递16个字节的字符串。如果只想解析部分字节,则可以执行以下操作:

fmt = 'hhl'
size = struct.calcsize(fmt)
struct.unpack(fmt, data[:size])

Additionally, your format string doesn't have a byte order, size and alignment specifier . 另外,您的格式字符串没有字节顺序,大小和对齐方式说明符 It is assumed to be "native" by default. 默认情况下假定为“本机”。 This means your code is system-dependent, which is probably not what you want for parsing a file format. 这意味着您的代码是与系统有关的,这可能不是解析文件格式所需要的。 You might need different alignments for different parts of the file. 您可能需要为文件的不同部分使用不同的对齐方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM