problem with struct module

Noah noah at noah.org
Tue Feb 24 14:42:11 EST 2004


Angelo Secchi <secchi at sssup.it> wrote in message news:<mailman.36.1077635852.8594.python-list at python.org>...
> I'm trying to use the unpack method in the struct module to parse a
> binary file without success. I have a binary file with records that

You did not mention what error you got, but I can guess that
it is because you are trying to unpack the structure inside an
infinite loop.

import struct
fmt='10s1960s'
size=struct.calcsize(fmt)
f=file("data")
s=f.read(size)
while s:
     print 'read:', s
     d1,d2=struct.unpack(fmt,s)
     print 'unpack', d1    
     s = f.read (size) ### YOU FORGOT THIS LINE.
     
Yours,
Noah
Spurrier



More information about the Python-list mailing list