简体   繁体   中英

Python: unicode .encode, can a function be called on unencodable characters?

I have a text, in uncode, that I'd like to encode in latin-1. Some characters cannot be encoded. If I use encode with the "replace" parameter, I get the question tag character, but, is there a way to call a custom function to replace the character?

For example, I'd like to convert all the possible characters to latin-1, and call unidecode.unidecode() on the unencodable characters. Is that possible?

You can create your own error handler with codecs.register_error('myerrorhandler', function) .

>>> import codecs
>>> codecs.register_error('silly', lambda e: ('X', e.start+1))
>>> 'foöbar'.encode('ascii', 'silly')
b'foXbar'
>>>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM