歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Python的struct使用注意

Python的struct使用注意

日期:2017/3/1 10:20:38   编辑:Linux編程

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位。

Copyright © Linux教程網 All Rights Reserved