Python struct.error:unpack需要一个长度为2的字符串参数

时间:2014-07-02 17:30:49

标签: python-2.7 io binary

我用字节格式的C ++编写了一些数据。我现在正在尝试使用Python再次读取该数据,但我遇到了错误;

Traceback (most recent call last):
  File "binary-reader.py", line 61, in <module>
    interaction_types.append(struct.unpack('<H',fp.read(2))[0]);
struct.error: unpack requires a string argument of length 2

我真的不明白,因为看起来我给的是长度为2的字符串,对吧?此外,我在第32行做了同样的事情

another question like mine但是没有答案是针对Python 3的。

这是我的代码

import sys
import struct
import os

print "Arguments : "
print str(sys.argv)

#N = #isects

#  2      2        3*4      2          3*4*N          4N            4N      3*4N        2N          2N
#imageX,imageY,throughput,#isects,isect_positions,primitive_ids,shape_ids,spectra,interaction_types,light_ids
file_path = str(sys.argv[1]);

byte_count = 0;
line_number = 1;

fp = open(file_path, "rb");
output = open('output.txt',"w");
file_size = os.path.getsize(file_path)

print "(input) file size = " + str(file_size);

while byte_count < file_size:
    print "Line number = " + str(line_number)
    print "Current byte count = " + str(byte_count)
    # Do stuff with byte.
    x = struct.unpack('<H', fp.read(2))[0]
    y = struct.unpack('<H', fp.read(2))[0]
    throughputOne = struct.unpack('<f', fp.read(4))[0]
    throughputTwo = struct.unpack('<f', fp.read(4))[0]
    throughputThree = struct.unpack('<f', fp.read(4))[0]
    nrIsects = struct.unpack('<H',fp.read(2))[0]

    # print "x = " + str(x)
    # print "y = " + str(y)
    # print "throughputOne = " + str(throughputOne)
    # print "throughputTwo = " + str(throughputTwo)
    # print "throughputThree = " + str(throughputThree)
    print "nrIsects = " + str(nrIsects)

    isect_positions = []
    for i in range(nrIsects*3):
        value = struct.unpack('<f',fp.read(4))[0]
        isect_positions.append(value);

    primitive_ids = []
    for i in range(nrIsects):
        value = struct.unpack('<I',fp.read(4))[0]
        primitive_ids.append(value);

    shape_ids = []
    for i in range(nrIsects):
        shape_ids.append(struct.unpack('<I',fp.read(4))[0]);

    spectra = []
    for i in range(nrIsects*3):
        spectra.append(struct.unpack('<f',fp.read(4))[0]);

    interaction_types = []
    for i in range(nrIsects):
        interaction_types.append(struct.unpack('<H',fp.read(2))[0]);

    light_ids = []
    for i in range(nrIsects):
        light_ids.append(struct.unpack('<H',fp.read(2))[0]);

    output_vars = [x,y,throughputOne,throughputTwo,throughputThree,nrIsects]

    line_string = ""

    for i in range(len(output_vars)):
        output.write(str(output_vars[i]))
        line_string += str(output_vars[i])
        if i is not len(output_vars) - 1:
            output.write(',')   
            line_string += ','

    print line_string


    #Update counters
    byte_count += 18 + 36*nrIsects
    line_number+=1

    # raw_input('Press any key to continue.');

    # print byte

这是一个要使用的输入文件的链接。您可以通过传递指定二进制文件路径的命令行参数来运行代码。我还用ASCII编写了代码,其中包含

0,0,[0.127076,0.127076,0.127076],1,{[0.144978,-0.294863,2.991749]},{3917},{3916},{[1.375603,1.375603,1.375603]},{5}, {0}

https://www.dropbox.com/s/tu1anqo5k0ygtd6/writetest.bin

编辑:我的文件布局可以在代码

中找到

1 个答案:

答案 0 :(得分:2)

在引发错误的fp.read(2)之前已经读取了50个字节。因此,fp.read(2)返回一个空字符串,struct.unpack引发异常:

In [83]: 2+2+4+4+4+2+12+4+4+12
Out[83]: 50

x = struct.unpack('<H', fp.read(2))[0]                  # 2 bytes read
y = struct.unpack('<H', fp.read(2))[0]                  # 2 bytes
throughputOne = struct.unpack('<f', fp.read(4))[0]      # 4 bytes
throughputTwo = struct.unpack('<f', fp.read(4))[0]      # 4 bytes
throughputThree = struct.unpack('<f', fp.read(4))[0]    # 4 bytes
nrIsects = struct.unpack('<H',fp.read(2))[0]            # 2 bytes

print "nrIsects = " + str(nrIsects)

isect_positions = []
for i in range(nrIsects*3):
    value = struct.unpack('<f',fp.read(4))[0]           # 12 bytes
    isect_positions.append(value)

primitive_ids = []
for i in range(nrIsects):
    value = struct.unpack('<I',fp.read(4))[0]           # 4 bytes
    primitive_ids.append(value)

shape_ids = []
for i in range(nrIsects):
    shape_ids.append(struct.unpack('<I',fp.read(4))[0]) # 4 bytes

spectra = []
for i in range(nrIsects*3):
    spectra.append(struct.unpack('<f',fp.read(4))[0])   # 12 bytes

interaction_types = []
for i in range(nrIsects):
    interaction_types.append(struct.unpack('<H', fp.read(2))[0])   # error!