By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,156 Members | 995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,156 IT Pros & Developers. It's quick & easy.

problem with struct module

P: n/a

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
include many fields for a total length of 1970. Few days ago I was
suggested by the list to use the struct module to parse it using the
following code in the hypothesis that for each records I have just two
fields:

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

Probably I was not even able to explain my problem since it seem that
this code is not able to read the file. Can anybody help me, maybe
asking for other infos they need? Also just suggest me a place where I
can find examples of using struc.unpack would be very helpful.
Thanks.
a.


--
================================================== ======
Angelo Secchi PGP Key ID:EA280337
================================================== ======
Current Position:
Graduate Fellow Scuola Superiore S.Anna
Piazza Martiri della Liberta' 33, Pisa, 56127 Italy
ph.: +39 050 883365
email: se****@sssup.it www.sssup.it/~secchi/
================================================== ======

Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Angelo,

Two things:

1) To open a file for reading binary,
I think you meant to write:

f=open("data","rb")

2) You don't need struct to decode a 10 character
string followed by a 1960 character string (10s1960s).
If they are really strings, just read them normally.
If they are binary data, then you should decode them
using struct, but you wouldn't use 10s1960s as format
you would use binary constructs in the format.

Perhaps more detail on what you "think" the structure
of the 1970 byte "records" is would help us help you.

-Larry
"Angelo Secchi" <se****@sssup.it> wrote in message
news:ma************************************@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
include many fields for a total length of 1970. Few days ago I was
suggested by the list to use the struct module to parse it using the
following code in the hypothesis that for each records I have just two
fields:

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

Probably I was not even able to explain my problem since it seem that
this code is not able to read the file. Can anybody help me, maybe
asking for other infos they need? Also just suggest me a place where I
can find examples of using struc.unpack would be very helpful.
Thanks.
a.


--
================================================== ======
Angelo Secchi PGP Key ID:EA280337
================================================== ======
Current Position:
Graduate Fellow Scuola Superiore S.Anna
Piazza Martiri della Liberta' 33, Pisa, 56127 Italy
ph.: +39 050 883365
email: se****@sssup.it www.sssup.it/~secchi/
================================================== ======

Jul 18 '05 #2

P: n/a
Angelo Secchi <se****@sssup.it> wrote in message news:<ma************************************@pytho n.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
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.