June 2017
Beginner
352 pages
8h 39m
English
To convert between bytes and str we must know the encoding of the byte sequence used to represent the string's Unicode code points as bytes. Python supports a wide-variety of so-called codecs such as UTF-8, UTF-16, ASCII, Latin-1, Windows-1251, and so on – consult the Python documentation for a current list of codecs
In Python we can encode a Unicode str into a bytes object, and going the other way we can decode a bytes object into a Unicode str. In either direction it's up to us to specify the encoding. Python won't — and generally speaking can't do anything to prevent you erroneously decoding UTF-16 data stored in a bytes object using, say, a CP037 codec for handling strings on legacy IBM mainframes.
If ...
Read now
Unlock full access