Python error

A638aa42130293f319eda7fa4ba121f4
0
fireside 141 Jun 20, 2013 at 04:23

I’m trying to figure out what the error is to this line:

binary_format="<"+str(name_length)+"s"
data=struct.pack(binary_format, self.name)

The error reads:
struct.error: the argument for ‘s’ must be a bytes object

struct is a module that was imported.

The line highlighted by the error shows the second line. Are they talking about the “s” that was added to the other symbols, and why would it need to be a byte object?
This apparently has something to do with going from python 2 to python 3. I tried converting binary format with bytes(binary_format,’utf-8’) but it didn’t work.

I tried this:

name_length=len(self.name)+1
binary_format= bytes("<",'utf-8')+ bytes(str(name_length),'utf-8')+ bytes("s",'utf-8')
data=struct.pack(binary_format, self.name)

but I still get the same error.

1 Reply

Please log in or register to post a reply.

A638aa42130293f319eda7fa4ba121f4
0
fireside 141 Jun 20, 2013 at 05:19

Found it. It was the self.name that needed to be converted to bytes, or maybe all of them. Anyway, it works.