problem with struct module

Larry Bates lbates at swamisoft.com
Tue Feb 24 12:03:35 EST 2004


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" <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
> 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: secchi at sssup.it www.sssup.it/~secchi/
> ========================================================
>





More information about the Python-list mailing list