problem with struct module
Angelo Secchi
secchi at sssup.it
Tue Feb 24 12:13:21 EST 2004
Apparently I solved my problem and this sample (in the Hypothesis that
I have$3$ fields for a total length of 1970) code works:
import struct
fmt='22s16s1932s'
size=struct.calcsize(fmt)
f=file('pan8997.finalevero','rb')
s=f.read(size)
d1,d2,d3=struct.unpack(fmt,line)
However I have now a different question that I was not able to solve
looking at the documentation. Suppose I am interested in a field that I
know starts at the 76th position and it is a 3s, How can I get it
directly without make some calculation and write
fmt='75s3s1892s'
Is there a way to say to implement in the format the statement "start
at the 76th position e return a string of length 3" ?
Thanks
a.
but actually I have a different question
On Tue, 24 Feb 2004 16:28:55 +0100
Angelo Secchi <secchi at sssup.it> wrote:
>
> 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.
>
>
>
More information about the Python-list
mailing list