Python Glossary Русский

all Python 3codecsfunction

codecs.getincrementalencoder()

incremental codecs

Returns an incremental encoder class: it encodes text in chunks and remembers state between calls, for example that the BOM has already been written.

Syntax

enc = codecs.getincrementalencoder(encoding)(errors='strict')

For UTF-8 there is almost no difference from str.encode, since it has no state. The difference shows with BOM encodings: encoding 'a' and 'b' separately with 'utf-16' puts two BOMs into the middle of a file, while an incremental encoder writes one. The same holds for 'utf-8-sig'. After reset() it treats the stream as new and writes the BOM again.

Examples

import codecs
enc = codecs.getincrementalencoder('utf-8-sig')()
print(enc.encode('a'), enc.encode('b'))  # → b'\xef\xbb\xbfa' b'b'
print('a'.encode('utf-8-sig'), 'b'.encode('utf-8-sig'))  # → b'\xef\xbb\xbfa' b'\xef\xbb\xbfb'

See also

Open in the full glossarydocs.python.orgReport an inaccuracy