all Python 3codecsfunction
codecs.decode()
encoding and decoding
Decodes an object with a codec looked up by name. For bytes to text bytes.decode is enough; codecs.decode is for codecs like base64, hex, zlib, rot13 and unicode_escape.
Syntax
codecs.decode(obj, encoding='utf-8', errors='strict')
For a plain bytes-to-text step this equals bytes.decode and gains nothing. It earns its place where bytes.decode cannot go: undoing base64 or hex, inflating zlib, turning the literal '\u043f' into a letter with unicode_escape. The result is not always str: decode(b'aGk=', 'base64') returns bytes. Without errors, damaged data raises UnicodeDecodeError; pass errors='replace' to read whatever survives.
Examples
import codecs
print(codecs.decode(b'aGk=\n', 'base64')) # → b'hi'
print(codecs.decode(b'6869', 'hex')) # → b'hi'
print(codecs.decode(b'\\u043f', 'unicode_escape')) # → п
import codecs
print(codecs.decode(b'ok\xff', 'utf-8', 'replace')) # → ok�
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy