Reading binary data

Aaron "Castironpi" Brady castironpi at gmail.com
Wed Sep 10 21:26:42 CEST 2008


On Sep 10, 1:12 pm, Aaron Scott <aaron.hildebra... at gmail.com> wrote:
> Sorry, I had posted the wrong error. The error I am getting is:
>
>      struct.error: unpack requires a string argument of length 12
>
> which doesn't make sense to me, since I'm specifically asking for 11.
> Just for kicks, if I change the line to
>
>      print struct.unpack('3sII', file.read(12))
>
> I get the result
>
>      ('GDE', 33554432, 16777216)
>
> ... which isn't even close, past the first three characters.

Sometimes 'endian' order can cause this.  Try '<3sII' and '>3sII' for
your formats to differentiate.

Also, if your file is not packed the way that 'struct' expects, you
might need to read the string and integers separately.

/Example:

>>> struct.Struct( '3s' ).size + struct.Struct( 'II' ).size
11
>>> struct.Struct( '3sII' ).size
12



More information about the Python-list mailing list