#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    Canada
    Posts
    51
    Rep Power
    2

    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
    Code:
    >>>import struct 
    >>>data = struct.pack('>i4shf', 2, b'spam', 3, 1.234)
    >>>data
    b'\x00\x00\x00\x02spam\x02spam\x00\x03?\9d\xf3\xb6
    >>>file = open('data.bin', 'wb')
    >>>file.write(data)
    14
    >>>file.close()
    but in this second part
    Code:
    import struct
    file = open('data.bin', 'rb')
    data = file.read()
    values = struct.unpack('>>i4shf', data)
    I get this error: struct.error: bad char in struct format.
    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.

    Thanks, Ryan
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    3
    Originally Posted by Good News
    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
    Code:
    >>>import struct 
    >>>data = struct.pack('>i4shf', 2, b'spam', 3, 1.234)
    >>>data
    b'\x00\x00\x00\x02spam\x02spam\x00\x03?\9d\xf3\xb6
    >>>file = open('data.bin', 'wb')
    >>>file.write(data)
    14
    >>>file.close()
    but in this second part
    Code:
    import struct
    file = open('data.bin', 'rb')
    data = file.read()
    values = struct.unpack('>>i4shf', data)
    I get this error: struct.error: bad char in struct format.
    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.

    Thanks, Ryan
    It probably was an error. ">" indicates Big Endian (bit order). I don't think ">>" means anything.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    Canada
    Posts
    51
    Rep Power
    2
    Originally posted by rrashkin
    It probably was an error. ">" indicates Big Endian (bit order). I don't think ">>" means anything.
    Thanks , thats what I thought. One thing I find confusing is on the errata page the author confirmed that it was '>>'
    Note from the Author or Editor: Yes -- change as described: page 152, 2nd code listing,
    line 4 should read as follows (change "bytes" at the end to "data"):
    values = struct.unpack('>>i4shf', data)

IMN logo majestic logo threadwatch logo seochat tools logo