Python Glossary Русский

all Python 3codecsfunction

codecs.iterencode()

incremental codecs

A generator that encodes an iterable of str chunks into bytes chunks with an incremental encoder, so the BOM and other state are written once per stream.

Syntax

for data in codecs.iterencode(texts, encoding, errors='strict'): ...

Use it when text is produced piece by piece and does not fit in memory. For UTF-8 it equals calling s.encode() per chunk, but not for 'utf-16' or 'utf-8-sig': per-chunk encode repeats the BOM before every piece. The input must be str; bytes raise TypeError, and the base64 and zlib codecs do not work here.

Examples

import codecs
parts = ['id;', 'имя']
print(b''.join(codecs.iterencode(parts, 'utf-8-sig')))  # → b'\xef\xbb\xbfid;\xd0\xb8\xd0\xbc\xd1\x8f'
print(b''.join(p.encode('utf-8-sig') for p in parts).count(codecs.BOM_UTF8))  # → 2

See also

Open in the full glossarydocs.python.orgReport an inaccuracy