Python Glossary Русский

all Python 3codecsfunction

codecs.replace_errors()

error handlers

The 'replace' error handler: when encoding it puts '?' in place of a character, when decoding it puts the U+FFFD sign '�' in place of bad bytes.

Syntax

codecs.replace_errors(exception)

The replacement differs by direction, which confuses people: '?' from encode is indistinguishable from a real question mark in the text. A '�' in output almost always means the bytes were decoded with the wrong encoding. The number of '�' signs does not equal the number of lost characters: one damaged UTF-8 sequence may yield several.

Examples

print('a€b'.encode('latin-1', 'replace'))  # → b'a?b'
print(b'a\xffb'.decode('utf-8', 'replace'))  # → a�b

See also

Open in the full glossarydocs.python.orgReport an inaccuracy