all Python 3codecsterm
codecs.StreamReader
stream codecs
The base class of stream decoders: it wraps a binary stream and offers read, readline and readlines returning str. codecs.getreader returns a ready subclass.
Syntax
reader = codecs.getreader(encoding)(stream, errors='strict')
It is the older alternative to io.TextIOWrapper and rarely appears in new code: TextIOWrapper is faster, translates newlines and supports tell/seek. StreamReader does not translate '\r\n' and splits lines its own way: readline also breaks on rare Unicode separators such as U+2028. You need it mostly for codecs without an incremental interface and for reading old code.
Examples
import codecs, io
raw = io.BytesIO('a\u2028b\n'.encode('utf-8'))
print(codecs.getreader('utf-8')(raw).readlines()) # → ['a\u2028', 'b\n']
raw.seek(0)
print(io.TextIOWrapper(raw, encoding='utf-8').readlines()) # → ['a\u2028b\n']
See also
Open in the full glossarydocs.python.orgReport an inaccuracy