Reading binary data

John Machin sjmachin at lexicon.net
Wed Sep 10 19:38:59 EDT 2008


On Sep 11, 4:16 am, 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 :(

Stop thrashing about, and do the following:
(1) print repr(open('test.gde, 'rb').read(100))
(2) tell us what you EXPECT to see in attr_val etc
(3) tell us what platform the file was created on and what platform
it's being read on
(4) (on the reading platform, at least) import sys; print
sys.byteorder

When showing results, do print ..., repr(attr_val)




More information about the Python-list mailing list