python-3.x - 结构错误 : unpack requires a buffer of 16 bytes

标签 python-3.x

我有这种格式的二进制文件 (b'A\xd9\xa5\x1ab\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x0b\xda\xa5\x1ab\x00\x00\x00\x02\x00\x00\x00\x02\x00\x00\x00\xcd\xdb\xa5\x1ab\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\xff\xdb\xa5\x1ab\x00\x00\x00\x05\x00\x00\x00\x01\x00\x00\x00\xe9\xdc\xa5\x1ab\x00\x00\x00\x06\x00\x00\x00\x02\x00\x00\x00\xf7\xdc\xa5\x1ab\x00\x00\x00\x08\x00\x00\x00\x02\x00\x00\x00\x1b\xdd\xa5\x1a')我将文件作为用户输入并在 read_file 中读取文件变量(类字节对象)。我需要使用整数模式(int,int,int,int)将其转换为 ascii 每个 int 4 个字节。我正在尝试使用 struct 来执行此操作库来解压它。我写了以下命令,但它给了我以下错误:

错误

print(unpack("IIII", read_file))
struct.error: unpack requires a buffer of 16 bytes

代码
    for (dirpath, dirnames, filenames) in walk('/Users/amathur1/PycharmProjects/learningpython/NAWF_VRG_G'):
        count = 1
        for file in filenames:
            print(count, " : ", file)
            count = count + 1
        print("select file you want to convert")
        input_file = input()
        print("Selected file number is : ", input_file)

        #To open the selected file
    with open(dirpath + "/" + filenames[int(input_file) - 1], 'rb') as file:
        # Reading the selected file i.e. file
        read_file = file.read()
    print(unpack("IIII", read_file))

最佳答案

您的文件似乎大于 4 个整数(16 字节)的大小;如果,如您所说,每组 4 个整数都需要转换,那么您必须将文件中的数据分解成一系列该大小的块。

关于python-3.x - 结构错误 : unpack requires a buffer of 16 bytes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51110525/

相关文章:

Python: 'NoneType' 对象不可下标'错误

python - 在 python 程序中运行 C 可执行文件

python - vscode python 转到符号不工作

python - 使用 ftplib 访问 FTP URL

python-3.x - Numpy 1.11 未安装在 virtualenv @ Ubuntu Studio 中

python - 我怎样才能让这个简单的Python登录程序循环起来?

python - python中的Numba jit警告解释

windows - 如何让Python PyQT程序在Windows中不打开命令行

python - 从在 python 中导入我的函数的文件中获取变量

python - Concurrent.futures 在命令行中运行良好,而不是在使用 pyinstaller 编译时