all Python 3codecsfunction
codecs.xmlcharrefreplace_errors()
error handlers
The 'xmlcharrefreplace' error handler: replaces an unencodable character with an HTML/XML reference like Ж. It works only for encoding.
Syntax
codecs.xmlcharrefreplace_errors(exception)
Handy for producing HTML in a narrow encoding: the browser still shows the right character. It does not work for decoding at all: bytes.decode(..., 'xmlcharrefreplace') raises TypeError. The reference is decimal, not named: 'Ж' becomes Ж, not &Zhcy;. In plain text rather than HTML such references mean nothing to a reader.
Examples
print('Ж€'.encode('latin-1', 'xmlcharrefreplace')) # → b'Ж€'
try:
b'\xff'.decode('utf-8', 'xmlcharrefreplace')
except TypeError:
print('only for encoding') # → only for encoding
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy