Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Struct.Error, Must Be a Bytes Object?

I am attempting to execute the code:

    values = (1, 'ab', 2.7)    
    s.struct.Struct('I 2s f')
    packed = s.pack(*values)

But I keep getting the error:

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    struct.error: argument for 's' must be a bytes object

Why? How do I fix this?

like image 677
Billjk Avatar asked Apr 10 '12 03:04

Billjk


1 Answers

With Python 3, 'ab' isn't a bytes object, what was called a str on Python 2, it's unicode. You need to use:

values = (1, b'ab', 2.7)

which tells Python that 'ab' is a byte literal. See PEP 3112 for more info.

like image 110
agf Avatar answered Sep 24 '22 11:09

agf