July 19th, 2013, 02:24 PM
Pack/unpack binary struct module
I'm currently working through prog. python 4th edition and theres a part I was wondering if someone could clarify.
The first code is packing, it was fine except the book example didn't have the b infront of spam which I guess is fine for 3.1
but in this second part
>>>data = struct.pack('>i4shf', 2, b'spam', 3, 1.234)
>>>file = open('data.bin', 'wb')
I get this error: struct.error: bad char in struct format.
file = open('data.bin', 'rb')
data = file.read()
values = struct.unpack('>>i4shf', data)
I fixed it by changing '>>i4shf' to '>i4shf' but this was not in the errata site for the book. If someone could offer any insight it would be a big help. I'm not sure sure what this first argument does.
July 19th, 2013, 02:43 PM
It probably was an error. ">" indicates Big Endian (bit order). I don't think ">>" means anything.
Originally Posted by Good News
July 19th, 2013, 03:12 PM
Thanks , thats what I thought. One thing I find confusing is on the errata page the author confirmed that it was '>>'