struct unpack newline

Fredrik Lundh fredrik at pythonware.com
Fri May 27 06:29:07 EDT 2005


grant at idscape.co.za wrote:

> I am pretty new to python and am having a problem
> intepreting binary data using struct.unpack.
> I am reading a file containing binary packed data
> using open with "rb". All the values are coming through
> fine when using (integer1,) = struct.unpack('l', line[86:90])
> except when line[86:90] contains "carriage-return" "linefeed"
> which are valid binary packed values. Error = unpack
> string size dows not match format. It seems that
> struct, instead of reading 4 bytes for line[86:90]
> only reads 2 bytes if the second byte is CR or LF.

verifying that struct doesn't care about newlines is of course
pretty trivial:

>>> import struct
>>> struct.unpack("l", "\0\0\0\0")
(0,)
>>> struct.unpack("l", "\0\r\0\0")
(3328,)
>>> struct.unpack("l", "\0\n\0\0")
(2560,)

have you verified that len(line) really is what you think?

>>> struct.unpack("l", "\0\r\n\0")
(658688,)
>>> struct.unpack("l", "\0\n\0")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
struct.error: unpack str size does not match format

</F> 






More information about the Python-list mailing list