467,919 Members | 1,209 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,919 developers. It's quick & easy.

problem with struct module


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
  • viewed: 1781
Share:
2 Replies
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
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.

Similar topics

2 posts views Thread by Matt Feinstein | last post: by
2 posts views Thread by Bryan Parkoff | last post: by
1 post views Thread by andychambers2002 | last post: by
2 posts views Thread by ajikoe | last post: by
2 posts views Thread by Jansson Christer | last post: by
reply views Thread by Keith | last post: by
reply views Thread by abarun22 | last post: by
5 posts views Thread by Neil Crighton | last post: by
8 posts views Thread by Bryan.Fodness | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.