简体   繁体   English

从二进制文件解包 4 字节无符号整数给出 struct.error:解包需要 4 字节的缓冲区

[英]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?如何解决这个问题?

Decoding a few integers解码几个整数

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])

Decoding a stream解码流

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.如果您需要从文件中解码任意长的整数流,有几个选项,具体取决于预期的数据长度。

Short stream短码流

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)

Long stream长流

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.arraynumpy.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.

相关问题 struct.error: unpack 需要 2 个字节的缓冲区 - struct.error: unpack requires a buffer of 2 bytes struct.error: unpack_from 需要至少 4 个字节的缓冲区 - struct.error: unpack_from requires a buffer of at least 4 bytes Python 3.8 结构解包 - struct.error: 解包需要 1 个字节的缓冲区 - Python 3.8 Struct unpacking - struct.error: unpack requires a buffer of 1 bytes struct.error: 解包 c++ 结构时需要 288 字节的缓冲区 - struct.error: unpack requires a buffer of 288 bytes when unpacking c++ structure struct.error: unpack 需要 1024 字节的缓冲区 - struct.error: unpack requires a buffer of 1024 bytes Django makemessages“struct.error:解包需要 4 个字节的缓冲区” - Django makemessages "struct.error: unpack requires a buffer of 4 bytes" Python 套接字 - struct.error:解包需要 4 字节的缓冲区 - Python socket - struct.error: unpack requires a buffer of 4 bytes python无法从以太网帧“struct.error: unpack requires a buffer of 20 bytes”中提取协议 - python unable to extract protocol from ethernet frame 'struct.error: unpack requires a buffer of 20 bytes' Pdfminer,struct.error:需要 x 字节的缓冲区 - Pdfminer, struct.error: requires buffer of x bytes struct.error:unpack需要长度为4的字符串参数 - 音频文件 - struct.error: unpack requires a string argument of length 4 - audio file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM