Python的struct使用注意

Python的struct模块可以让我们很方便的操作二进制数据,但是我们必须注意的是:我们在使用struct进行二进制操作的时候会发现,操作系统和硬件将影响程序是否正常运行。

Format = 'lllllfll'
 f = open('test.dat','rb')
 data = f.read(32)
 s=[]
 s.append(struct.unpack(Format,data))

32位下正常,64位下报:“struct.error: unpack requires a string argument of length 64”
同样是64位的操作系统,Windows和UNIX行为可能不太一样。UNIX上的long可能是64位,Windows可能就是32位。

python语言的整形相当于C语言中的long型,在32位机器上位宽为32位,在64位系统上,整型的位宽为64位。

相关推荐