Sunday 15 May 2011

python 3.3: struct.pack won't accept strings -


I am trying to use struct.pack to write a padded string in a file but it does not have to be 3.x Interpreters does not work anymore. An example of how I am using it:

  mystring = anotherstring + "some time ago" output = straight pocket ("30s", meststring);   

This is fine in previous versions of Python, but with 3 it generates an error to demand a byte object. Docs means that it appears to convert any string into a UTF-8 byte object without complaining (and I do not care if multi-byte characters have to be shortened):

: "C, S and P conversion codes work on objects bytes, but packing with such codes also supports the str object, which are encoded using UTF-8."

How do I read other documents that are using struct.pack along with strings correctly reading documents?

Yes, up to 3.1 Struct.pack () incorrectly utf -Instead of strings on 8 bytes; It was fixed in Python 3.2. See.

The conclusion was that the underlying conversion was a bad idea, and it was returned, while developers still got the chance to do this:

I like to break I'm looking forward to maintaining a broken API for 10 or 20 years today :-) And we have a very small user base that is using Python 3, it is now easier to change it than the next release .

Also in the document:

struct.pack () now only s string Allows bytes for pack codes. In the past, this text will accept logic and using UTF-8 will give the byte an indication form. It was problematic because it was creating perceptions about correct encoding and because a variable-length encoding could fail when writing a fixed length segment of the structure.

You must clearly encode your stars before packing.

No comments:

Post a Comment