Unpack Expects the Wrong Number of Bytes

Ross Ridge rridge at csclub.uwaterloo.ca
Sat Jul 18 10:55:53 EDT 2009


Timothy Crone  <tjcrone at gmail.com> wrote:
>header = "s"
>data = list(unpack(header,f.read(1)))
>
>however this:
>
>header = "si"
>data = list(unpack(header,f.read(5)))
>
>throws
>
>struct.error: unpack requires a string argument of length 8
>
>So unpack expects 7 additional bytes when an integer is added to the
>format string. Does anyone know what is going on?

It's adding pad bytes so that the format matches the equivilent C
structure on your machine.  You should use either the "<" little-endian
or the ">" big-endian prefix depending on the byte order used in the file
you're trying to unpack.  You can also use the "=" native byte-order
flag if endianness of the file format changes according to the machine
your Python program runs on.  If you use any of these flags, no extra
padding will be inserted.

				Ross Ridge

-- 
 l/  //	  Ross Ridge -- The Great HTMU
[oo][oo]  rridge at csclub.uwaterloo.ca
-()-/()/  http://www.csclub.uwaterloo.ca/~rridge/ 
 db  //	  



More information about the Python-list mailing list