The contents of files and network messages may represent encoded characters. They often need to be converted to unicode for proper display.
In Python 2, you may need to convert str data to Unicode characters. The default (
"", etc.) is an ASCII string, with any values outside of ASCII range displayed as escaped values. Unicode strings are
# You get "© abc" encoded in UTF-8 from a file, network, or other data source s = '\xc2\xa9 abc' # s is a byte array, not a string of characters # Doesn't know the original was UTF-8 # Default form of string literals in Python 2 s # '\xc2' - meaningless byte (without context such as an encoding) type(s) # str - even though it's not a useful one w/o having a known encoding u = s.decode('utf-8') # u'\xa9 abc' # Now we have a Unicode string, which can be read as UTF-8 and printed properly # In Python 2, Unicode string literals need a leading u # str.decode converts a string which may contain escaped bytes to a Unicode string u # u'\xa9' - Unicode Character 'COPYRIGHT SIGN' (U+00A9) '©' type(u) # unicode u.encode('utf-8') # '\xc2\xa9 abc' # unicode.encode produces a string with escaped bytes for non-ASCII characters
In Python 3 you may need to convert arrays of bytes (referred to as a 'byte literal') to strings of Unicode characters. The default is now a Unicode string, and bytestring literals must now be entered as
b"", etc. A byte literal will return
isinstance(some_val, byte), assuming
some_val to be a string that might be encoded as bytes.
# You get from file or network "© abc" encoded in UTF-8 s = b'\xc2\xa9 abc' # s is a byte array, not characters # In Python 3, the default string literal is Unicode; byte array literals need a leading b s # b'\xc2' - meaningless byte (without context such as an encoding) type(s) # bytes - now that byte arrays are explicit, Python can show that. u = s.decode('utf-8') # '© abc' on a Unicode terminal # bytes.decode converts a byte array to a string (which will, in Python 3, be Unicode) u # '\u00a9' - Unicode Character 'COPYRIGHT SIGN' (U+00A9) '©' type(u) # str # The default string literal in Python 3 is UTF-8 Unicode u.encode('utf-8') # b'\xc2\xa9 abc' # str.encode produces a byte array, showing ASCII-range bytes as unescaped characters.