liveBook · Manning

UTF-8

As explained in The Quick Python Book, Fourth Edition

UTF-8 is a variable-length character encoding that represents Unicode text while preserving the standard ASCII byte representations.

UTF-8 is a Unicode encoding that preserves basic ASCII characters while supporting a very broad range of characters and symbols. This makes it a practical default for reading ordinary text files: ASCII-only files remain compatible, while UTF-8-encoded accented letters, punctuation, and symbols are also covered. section 21.2.1

Encoding matters because a byte outside the ASCII range is ambiguous without knowing the encoding that produced it. Python 3 strings handle Unicode by default, and open() uses strict error handling unless another errors policy is selected. section 21.2.1

Choose an error policy when file contents cannot be decoded successfully:

  • strict raises an error as soon as an encoding problem is encountered. section 21.2.1
  • ignore skips characters that cause encoding errors. section 21.2.1
  • replace substitutes a marker character, while backslashreplace preserves an escape-style representation. section 21.2.1
  • surrogateescape retains offending bytes through private Unicode code points and can translate them back when writing. section 21.2.1

Reading invalid UTF-8 bytes

A file containing the bytes 65, 66, 67, 255, 192, and 193 begins as ABC but contains invalid UTF-8 bytes. A normal read raises UnicodeDecodeError. Using open('test.txt', errors='ignore').read() returns 'ABC'; errors='replace' returns replacement markers; errors='surrogateescape' returns '\udcff\udcc0\udcc1'; and errors='backslashreplace' returns '\\xff\\xc0\\xc1'. section 21.2.1

open('test.txt', 'wb').write(bytes([65, 66, 67, 255, 192,193])) 

Inspect the raw byte sequence used to demonstrate how non-ASCII values can trigger UTF-8 decoding errors. section 21.2.1

open('test.txt', errors='ignore').read()
 
'ABC'
 
open('test.txt', errors='replace').read()
 
'ABC   '                                       #1
 
open('test.txt', errors='surrogateescape').read()
 
'ABC\udcff\udcc0\udcc1'
 
open('test.txt', errors='backslashreplace').read()
 
'ABC\\xff\\xc0\\xc1'
#1 Replaced error characters with chr(65533)

Compare the visible results produced by ignore, replace, surrogateescape, and backslashreplace. section 21.2.1

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Explains ASCII compatibility, Unicode coverage, decoding ambiguity, and error policies, but does not detail UTF-8 byte-formation rules.
Examples
Uses raw invalid bytes, a UnicodeDecodeError, and four open() error policies with contrasting returned strings.
What it assumes
Assumes general programming knowledge while establishing the practical distinction between ASCII, Unicode, bytes, and text decoding.
What you can do after
Operate Python file reads with UTF-8 assumptions and select an errors policy for malformed input.
How it connects
Connects ASCII, Unicode, Python 3 strings, file I/O, decoding errors, and ETL data processing.

Also explained in

Compare how 11 books explain UTF-8 →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.