Python Glossary Русский

all Python 3codecsfunction

codecs.backslashreplace_errors()

error handlers

The 'backslashreplace' error handler: replaces a problem character or byte with an escape like \xe9, \u20ac or \xff.

Syntax

codecs.backslashreplace_errors(exception)

The best choice for logs and diagnostics: nothing is lost, and the original can be reconstructed from the code. Since 3.5 it also works when decoding, turning a bad byte into '\xff'. sys.stderr uses it by default, which is why printing to stderr never fails on a character. The result does not turn back into the original text by itself.

Examples

print('café €'.encode('ascii', 'backslashreplace'))  # → b'caf\\xe9 \\u20ac'
print(b'ok\xff'.decode('utf-8', 'backslashreplace'))  # → ok\xff

See also

Open in the full glossarydocs.python.orgReport an inaccuracy