Reading binary data

Aaron Scott aaron.hildebrandt at gmail.com
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 = 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.



More information about the Python-list mailing list