all Python 3codecsterm
codecs
module
The codec registry: text encoding and decoding, error handlers and BOM markers.
Syntax
import codecs
Usually str.encode, bytes.decode and open with encoding are enough — codecs goes deeper: look up a codec by name, decode a stream in chunks with an incremental decoder, register your own error handler. It also holds the BOM constants and bytes-to-bytes codecs such as hex. The codecs.open function is deprecated since Python 3.14 in favour of the built-in open.
Examples
import codecs
print(codecs.lookup('UTF8').name) # → utf-8
print(codecs.encode(b'Hi', 'hex')) # → b'4869'
dec = codecs.getincrementaldecoder('utf-8')()
print(repr(dec.decode(b'\xd0')), dec.decode(b'\xaf')) # → '' Я
See also
Open in the full glossarydocs.python.orgReport an inaccuracy