我如何去在Python打开二进制数据文件,并在同一时间读回值一个long
,进入结构。我目前所面对的是这样的,但我认为这将继续覆盖idList
,我要追加到它,所以我结束了所有的文件中long
值的元组 -阅读Python中的二进制文件到一个结构
file = open(filename, "rb")
try:
bytes_read = file.read(struct.calcsize("=l"))
while bytes_read:
# Read 4 bytes(long integer)
idList = struct.unpack("=l", bytes_read)
bytes_read = file.read(struct.calcsize("=l"))
finally:
file.close()
感谢。
感谢。不幸的是,我们目前仅限于使用Python 2.5,这与此有何不同? – 2010-05-19 14:04:46
@Adam,只需在模块的开头添加'from __future__ import with_statements'即可。 – 2010-05-19 14:06:16
在数组示例中,您从文件调用的值为2000,如果不是4,那么对于四字节整数?或者我误解了这个功能? – 2010-05-19 14:19:36