standards were created by different people around the world Many were extending ASCII in different ways This created “bilingual encodings”, but not “multilingual” The Unicode Standard was created to unify them
Units in UTF-32 LATIN SMALL LETTER A a a U+0061 0x61 0x00000061 LATIN SMALL LETTER SHARP S ß U+00DF 0xDF 0x000000DF HAN FOR EAST 東 U+6771 0x67 0x71 0x00006771 DESERET CAPITAL LETTER LONG I U+10400 0xF0 0x90 0x90 0x80 0x00010400
encode() bytes.decode(encoding, errors) -> str str.encode(encoding, errors) -> bytes First parameter: encoding (how to translate from code point to bytes/reverse) Second parameter: errors (how to handle unrecognized code points)
between bytes ⇔ str Defaults to UTF-8, can specify alternative encoding errors parameter specifies how to handle unrecognized code points: ignore - skip the code point strict - raise an Exception replace - replace the unknown code point with something, e.g. �
text? There is a specified encoding, but it doesn’t match/work/spits out mojibake? There is just no way to specify the encoding in that document/file/whatever? It happens a lot: Scraping websites Reading/parsing email
as a last resort when: It’s HTTP and there is no charset parameter in the Content-Type header It’s HTML and there is no <meta http-equiv="content-type"> It’s XML and there is no encoding attribute in the prolog It’s something else and there is no explicit character encoding