Struggling with struct.unpack() and "p" format specifier

Peter Hansen peter at engcorp.com
Tue Nov 30 10:37:17 EST 2004


Geoffrey wrote:
> I am trying to read data from a file binary file and then unpack the
> data into python variables.  Some of the data is store like this;
...
> As I read the documentation the "p" format string seems to address
> this situation, where the number bytes of the string to read is the
> first byte of the stored value but I keep getting this error.
> 
> Am I missing something ?
> Can the "p" format character be used to unpack this type of data ?

I've tried experimenting with "p" and cannot get any meaningful
results.  In all cases pack() returns '\x00' while unpack()
with anything other than a one-byte string returns an exception
(unpack str size does not match format) while with a one-byte
string it always returns ('',).

I would be inclined to say that the "p" format in struct (using
Python 2.4rc1 or Python 2.3.3) does not act as documented on
Windows XP SP2, at least...

I hope we've both just missed something obvious.

-Peter



More information about the Python-list mailing list