Binary and Encoding Modules

codecs

Look up codecs and use encoding, decoding and incremental conversion interfaces. Some registered codecs transform bytes to bytes or text to text. For ordinary encoded text files use open() with an explicit encoding. Strict error handling is preferable when loss is unacceptable.

Formal Syntax & Structure

python syntaxpython
import codecs

Return Value

module

Module namespace available through import; import is a statement, not a value-returning expression.

Working Example

Example Snippetpython
import codecsdecoder = codecs.getincrementaldecoder("utf-8")()print(repr(decoder.decode(b"\xc3", final=False)))print(decoder.decode(b"\xa9", final=True))
Python Reference