Python 3.5+codecsfunction
codecs.namereplace_errors()
error handlers
The 'namereplace' error handler: replaces an unencodable character with its Unicode name as \N{...}. It works only for encoding.
Syntax
codecs.namereplace_errors(exception)
The most human-readable option: a log shows \N{EURO SIGN} instead of \u20ac. The form matches Python string-literal syntax, so the result can be pasted into code. The cost is length: every character turns into dozens of bytes. Added in 3.5; like xmlcharrefreplace, it raises TypeError when decoding.
Examples
print('5€'.encode('ascii', 'namereplace')) # → b'5\\N{EURO SIGN}'
print('5\N{EURO SIGN}') # → 5€
See also
Open in the full glossarydocs.python.orgReport an inaccuracy