9

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?

agf
178k45 gold badges300 silver badges241 bronze badges
asked Apr 10, 2012 at 3:05

1 Answer 1

25

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.

answered Apr 10, 2012 at 3:09
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.