Reading binary data

Jon Clements joncle at googlemail.com
Wed Sep 10 15:13:52 EDT 2008


On Sep 10, 7:16 pm, Aaron Scott <aaron.hildebra... at gmail.com> wrote:
> Taking everything into consideration, my code is now:
>
> import struct
> file = open("test.gde", "rb")
> signature = file.read(3)
> version, attr_count = struct.unpack('II', file.read(8))
> print signature, version, attr_count
> for idx in xrange(attr_count):
>         attr_id, attr_val_len = struct.unpack('II', file.read(8))
>         attr_val = file.read(attr_val_len)
>         print attr_id, attr_val_len, attr_val
> file.close()
>
> which gives a result of:
>
> GDE 2 2
> 1 4 é
> 2 4 ê Å
>
> Essentially, the same results I was originally getting :(

Umm, how about yourfile.read(100)[or some arbitary value, just to see
the data) and see what it returns... does it return something that
looks like values you'd expect in a char[]... I also find it odd that
the attr_val_len appears to be 4?



More information about the Python-list mailing list