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

Geoffrey geskerrett at hotmail.com
Wed Dec 1 09:20:48 EST 2004


Thanks for your response.

I guess the documentation on the p format wasn't clear to me ... or
perhaps I was just hoping to much for an easy solution !

The data is part of a record structure that is written to a file with
a few "int"'s and "longs" mixed in.  The pattern repeats through the
file with sometime up to 2500 repititions.

Clearly I can create a subroutine to read the records and extract out
the fields.  I was just hoping I could use the "struct" module and
create a pattern like 'LLHpHLpppH' which would unpack the date and
automatically give me the strings without needing to first determine
their lengths as the length is already embedded in the data.

Any suggestion on how to go about proposing the ability to read
variable length strings based on the preceeding byte value to the
struct module ?  It seems it would be a valuable addition, helping
with code clarity, readability and saving quite a few lines of code -
well atleast me anyways !

Thanks again.

Peter Hansen <peter at engcorp.com> wrote in message news:<coi4o4$8in$1 at utornnr1pp.grouptelecom.net>...
> Geoffrey wrote:
> > As I mentioned, I can parse the string and read it with multiple
> > statements, I am just looking for a more efficient solution.
> 
> This looks like about the best you can do, using the information
> from Tim's reply:
> 
>  >>> buf = '\0\0\xb9\x02\x13EXCLUDE_CREDIT_CARD'
>  >>> import struct
>  >>> x = struct.unpack('>xxBB%sp' % (ord(buf[4])+1), buf)
>  >>> x
> (185, 2, 'EXCLUDE_CREDIT_CARD')
> 
> If you wanted to avoid hard-coding the 4, you would
> be most correct to do this:
> 
> header = '>xxBB'
> lenIndex = struct.calcsize(header)
> x = struct.unpack('%s%dp' % (header, ord(buf[lenIndex])+1), buf)
> 
> ... though that doesn't exactly make it all that readable.
> 
> -Peter



More information about the Python-list mailing list