all Python 3ioterm
io.IncrementalNewlineDecoder
utilities
An incremental decoder that translates line endings to '\n' (universal newlines) as bytes arrive; text streams use it.
Syntax
io.IncrementalNewlineDecoder(decoder, translate)
The decoder carries state between calls: if a chunk ends with '\r' it holds that character back until the next call, because it cannot yet tell a lone newline from half of '\r\n'. So decode() may hand back less than you fed it, and the stream must be finished with a final=True call. You rarely instantiate it yourself — it is the helper TextIOWrapper uses for files opened with newline=None.
Examples
import io, codecs
base = codecs.getincrementaldecoder('utf-8')()
d = io.IncrementalNewlineDecoder(base, translate=True)
print(repr(d.decode(b'a\r\nb'))) # → 'a\nb'
See also
Open in the full glossarydocs.python.orgReport an inaccuracy