all Python 3codecsfunction
codecs.getdecoder()
codec registry
Returns a codec's stateless decoder function: it takes bytes and returns a pair (string, number of bytes consumed). Same as lookup(name).decode.
Syntax
decode = codecs.getdecoder(encoding)
The result is a tuple, not str, a common slip when replacing bytes.decode. A stateless decoder cannot wait for more input: a multi-byte character split by a chunk boundary from a socket or file raises UnicodeDecodeError. For chunked data use getincrementaldecoder or iterdecode; for complete bytes, bytes.decode is enough.
Examples
import codecs
dec = codecs.getdecoder('utf-8')
print(dec(b'abc')) # → ('abc', 3)
chunk = 'я'.encode('utf-8')[:1]
try:
dec(chunk)
except UnicodeDecodeError as e:
print(e.reason) # → unexpected end of data
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy