Tutorial by Examples

In Python 3 str is the type for unicode-enabled strings, while bytes is the type for sequences of raw bytes. type("f") == type(u"f") # True, <class 'str'> type(b"f") # <class 'bytes'> In Python 2 a casual string was a sequence of raw byte...
.encode and .decode both have error modes. The default is 'strict', which raises exceptions on error. Other modes are more forgiving. Encoding >>> "£13.55".encode('ascii', errors='replace') b'?13.55' >>> "£13.55".encode('ascii', errors='ignore') b'13.55' ...
Files opened in a non-binary mode (e.g. 'r' or 'w') deal with strings. The deafult encoding is 'utf8'. open(fn, mode='r') # opens file for reading in utf8 open(fn, mode='r', encoding='utf16') # opens file for reading utf16 # ERROR: cannot write bytes when a string is expecte...

Page 1 of 1