all Python 3codecsfunction
codecs.iterdecode()
incremental codecs
A generator that decodes an iterable of bytes chunks into str chunks with an incremental decoder, without breaking characters at chunk boundaries.
Syntax
for text in codecs.iterdecode(chunks, encoding, errors='strict'): ...
A handy wrapper over getincrementaldecoder for a loop over chunks, for example over iter(lambda: f.read(4096), b''). The text pieces it yields need not match the input chunks one to one, so join them with ''.join(...) instead of relying on a mapping. The codec must decode bytes: rot13 does not fit. If you can open the file with open(..., encoding=...), that is simpler.
Examples
import codecs
data = 'Привет'.encode('utf-8')
chunks = [data[:3], data[3:7], data[7:]]
print(list(codecs.iterdecode(chunks, 'utf-8'))) # → ['П', 'ри', 'вет']
print(''.join(codecs.iterdecode(chunks, 'utf-8'))) # → Привет
See also
Open in the full glossarydocs.python.orgReport an inaccuracy