String packing unpacking in Python

时间:2015-10-06 09:03:33

标签: python string

I am facing the following error.

Traceback (most recent call last):
  File "Thread_main.py", line 681, in <module>

    fdata = struct.unpack('f', str(AObytes[i:i+3]))

struct.error: unpack requires a string argument of length 4

Here is the code line which gives error.

for i in range(640,8196,4):             
### === This AO bytes mapping is till for all AOS data , Last bytes are   8192,8193,8194,8195
fdata = struct.unpack('f', str(AObytes[i:i+4]))
AOSID[aop1] = fdata[0]                      
aop1 =aop1+1        

I have defined Aobytes as this at the top. AObytes = [0]*8200

1 个答案:

答案 0 :(得分:0)

str(AObytes[i:i+4])是“[a,b,c,d]”所以它是12个或更多的字符串......

也许你想要这样的东西:

fdata = struct.unpack('f', " ".join(str(x) for x in AObytes[i:i+4]))
fdata = fdata.split()

希望得到这个帮助。