[英]Unpacking 4-byte unsigned integers from binary file gives struct.error: unpack requires a buffer of 4 bytes
This is a repeated question but I couldn't find answer这是一个重复的问题,但我找不到答案
I am reading a binary file in following format (just showing one of the 3000 lines in hex format as shown in sublime text):我正在读取以下格式的二进制文件(仅显示 3000 行十六进制格式之一,如 sublime text 所示):
0009 7f71 0009 b87b 0009 f24b 000a 2ce2
I want to read it as tuple of 4-byte unsigned integers我想将它读作 4 字节无符号整数的元组
if filename:
with open(filename, mode='rb') as file:
fileData = file.read()
unsignedData = struct.unpack('I', fileData )
However I get following error for last line in above code:但是,我在上面的代码中的最后一行出现以下错误:
struct.error: unpack requires a buffer of 4 bytes
How to fix this?如何解决这个问题?
The io.RawIOBase.read
will usually return a lot more than 4 bytes (it's limited by the system call and/or file size). io.RawIOBase.read
通常会返回超过 4 个字节(它受系统调用和/或文件大小的限制)。
On the other hand, the buffer's size in bytes must match the size required by the format string to struct.unpack
.另一方面,缓冲区的字节大小必须与struct.unpack
格式字符串所需的大小相匹配。
The overall data structure of your file is not clear, but to, for example, read 4 unsigned 32-bit integers encoded in little-endian (data you provided), you should slice the buffer:您的文件的整体数据结构尚不清楚,但例如,要读取以 little-endian 编码的 4 个无符号 32 位整数(您提供的数据),您应该对缓冲区进行切片:
unsignedData = struct.unpack('4I', fileData[:16])
In case you need to decode an arbitrarily long stream of integers from your file, there are a few options, depending on the expected data length.如果您需要从文件中解码任意长的整数流,有几个选项,具体取决于预期的数据长度。
with open(filename, mode='rb') as fp:
fileData = fp.read()
n, r = divmod(len(fileData), struct.calcsize('I'))
assert r == 0, "Data length not a multiple of int size"
unsignedData = struct.unpack('I' * n, fileData)
You could use struct.iter_unpack
, but it probably makes more sense to keep the large int data in an array.array
or numpy.array
anyway.您可以使用struct.iter_unpack
,但无论如何将大型 int 数据保存在array.array
或numpy.array
可能更有意义。
Here's an example for a NumPy array:这是 NumPy 数组的示例:
import numpy
data = numpy.fromfile(filename, dtype='uint32')
I'll leave the loading into Python's homogeneous data array as an exercise to the reader (hint: array.fromfile
).我将把加载到 Python 的同构数据数组中作为练习array.fromfile
读者(提示: array.fromfile
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.