Reading binary data

Aaron Scott aaron.hildebrandt at
Wed Sep 10 19:14:13 CEST 2008

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 =
print output
version = struct.unpack("I",[0]
print version
attr_count = struct.unpack("I",[0]
while attr_count:
	print "---", 1)
	counter = int(struct.unpack("I",[0])
	attr_count -= 1

Of course, this doesn't work at all. It produces:

ê Å

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.

More information about the Python-list mailing list