Struct.error: unpack 需要 16 字节的缓冲区

New*_*der 5 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 ) 每个int4 个字节。我正在尝试使用struct库来解压缩它。我写了以下命令,但它给了我以下错误:

错误

print(unpack("IIII", read_file))
struct.error: unpack requires a buffer of 16 bytes
Run Code Online (Sandbox Code Playgroud)

代码

    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))
Run Code Online (Sandbox Code Playgroud)

Sco*_*ter 5

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