Reading binary data

Jon Clements joncle at googlemail.com
Wed Sep 10 13:33:18 EDT 2008


On 10 Sep, 18:14, Aaron Scott <aaron.hildebra... at gmail.com> wrote:
> I've been trying to tackle this all morning, and so far I've been
> completely unsuccessful. I have a binary file that I have the
> structure to, and I'd like to read it into Python. It's not a
> particularly complicated file. For instance:
>
> signature   char[3]     "GDE"
> version     uint32      2
> attr_count  uint32
> {
>     attr_id         uint32
>     attr_val_len    uint32
>     attr_val        char[attr_val_len]
>
> } ... repeated attr_count times ...
>
> However, I can't find a way to bring it into Python. This is my code
> -- which I know is definitely wrong, but I had to start somewhere:
>
> import struct
> file = open("test.gde", "rb")
> output = file.read(3)
> print output
> version = struct.unpack("I", file.read(4))[0]
> print version
> attr_count = struct.unpack("I", file.read(4))[0]
> while attr_count:
>         print "---"
>         file.seek(4, 1)
>         counter = int(struct.unpack("I", file.read(4))[0])
>         print file.read(counter)
>         attr_count -= 1
> file.close()
>
> Of course, this doesn't work at all. It produces:
>
> GDE
> 2
> ---
> é
> ---
> ê Å
>
> I'm completely at a loss. If anyone could show me the correct way to
> do this (or at least point me in the right direction), I'd be
> extremely grateful.

What if we view the data as having an 11 byte header:
signature, version, attr_count = struct.unpack('3cII',
yourfile.read(11))

Then for the list of attr's:
for idx in xrange(attr_count):
    attr_id, attr_val_len = struct.unpack('II', yourfile.read(8))
    attr_val = yourfile.read(attr_val_len)


hth, or gives you a pointer anyway
Jon.





More information about the Python-list mailing list