I want to make python ignore chars it can’t encode, by simply replacing them with the string "<could not encode>".
E.g, assuming the default encoding is ascii, the command
'%s is the word'%'ébác'
would yield
'<could not encode>b<could not encode>c is the word'
Is there any way to make this the default behavior, across all my project?
The
str.encodefunction takes an optional argument defining the error handling:From the docs:
In your case, the
codecs.register_errorfunction might be of interest.[Note about bad chars]
By the way, note when using
register_errorthat you’ll likely find yourself replacing not just individual bad characters but groups of consecutive bad characters with your string, unless you pay attention. You get one call to the error handler per run of bad chars, not per char.