Python 3.3: struct.pack will not accept strings

I am trying to use struct.pack to write a completed string to a file, but it seems to be not working with 3.x interpreters anymore. An example of how I use it:

mystring = anotherString+" sometext here" output = struct.pack("30s", mystring); 

In earlier versions of python, this looks fine, but with 3 it causes an error requiring a byte object. The docs seem to imply that he should do the conversion of any string to a UTF-8 byte object without complaint (and I don't care if the multibyte character is truncated):

http://docs.python.org/release/3.1.5/library/struct.html : "Conversion codes c, s and p work with byte objects, but packaging with such codes also supports str objects that are encoded using UTF -8".

Am I reading documents incorrectly and how do others use struct.pack with strings?

+7
source share
2 answers

Yes, before 3.1, struct.pack() will erroneously encode strings into UTF-8 bytes; this has been fixed in Python 3.2. See question 10783 .

The conclusion was that implicit conversion was a bad idea, and it was canceled, while developers still had the opportunity to do this:

I prefer to break the API today than support a broken API for 10 or 20 years :-) And we have a very small user base using Python 3, it is now easier to change it than in the next version.

This is also described in the port section of manual 3.2 What's New :

struct.pack() now only allows bytes for s string code. He used to take text arguments and implicitly encode them into bytes using UTF-8. This was problematic because it made assumptions about the correct encoding and because variable length encoding may not work when writing to a segment of a fixed length structure.

You need to explicitly encode your lines before packing.

+10
source

I could be wrong, but .encode('UTF-8') will not work in this case? eg:

 output = struct.pack("30s", mystring.encode('UTF-8')); 

I correct.

0
source

All Articles