我已经使用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

编辑:我的文件的布局可以在代码中作为注释找到

最佳答案

引发错误的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!

10-05 20:01