[Tutor] binary data struct module

John washakie at gmail.com
Tue Sep 4 01:37:57 CEST 2007


Hello,

I have an unformatted mixed type binary file I'm trying to read into Python.
So far, I've gotten as far as:

f2=file(infile,'rb')

Dfmt=['3i','13s','7i','2f','2i','2f','2i','i']  #format for binary reading
first bits

if f2:
    print infile + ' has been opened'
    #for ft in Dfmt:
    #  print ft
    a=(struct.unpack(ft,f2.read(struct.calcsize(ft))) for ft in Dfmt)
    for ln in a: print ln

Which gives me:

/cygdrive/c/washakie/binfile has been opened
(21, 20060612, 0)
('Version 4.3',)
(21, 12, -86400, -86400, -900, 12, 24)
(-179.0, -90.0)
(360, 180)
(1.0, 1.0)
(24, 16)
(3,)

however, how can I now assign variables based on the 'generator object' a?
What exactly is this? When I try to do something like:

print a[0]
or
print a(0)

I get the error:
Traceback (most recent call last):
  File "readheader.py", line 20, in <module>
    print a[0]
TypeError: 'generator' object is unsubscriptable

My question is, how can I now work with 'a' so that i can use the values?

Thanks!



-- 
Configuration
``````````````````````````
Plone 2.5.3-final,
CMF-1.6.4,
Zope (Zope 2.9.7-final, python 2.4.4, linux2),
Five 1.4.1,
Python 2.4.4 (#1, Jul 3 2007, 22:58:17) [GCC 4.1.1 20070105 (Red Hat
4.1.1-51)],
PIL 1.1.6
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070903/be71470e/attachment.htm 


More information about the Tutor mailing list