all Python 3codecsterm
encodings
module
The codec package: it holds the implementations of all built-in encodings found by str.encode, bytes.decode and open(encoding=...).
Syntax
import encodings
encodings is rarely imported directly: it stores codecs such as encodings.utf_8 and encodings.cp1251, while the codecs module and the built-in encode/decode do the work. Submodules with special behaviour are encodings.idna for domain names and encodings.utf_8_sig, which strips the BOM. Encoding names are normalised, so 'UTF-8', 'utf8' and 'utf_8' are one codec.
Examples
import encodings, codecs
print(encodings.normalize_encoding('UTF-8 sig')) # → UTF_8_sig
print(codecs.lookup('utf8').name) # → utf-8
print('я'.encode('utf_8_sig')) # → b'\xef\xbb\xbf\xd1\x8f'
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy