The error is happening when Python tries to print. When printing, that is writing to sys.stdout
, Python encodes the text to be printed with the encoding expected by the terminal. In this case the system encoding is gbk, but gbk is unable to encode the third character in the string ('\ue13b'
), so the UnicodeEncodeException
is raised.
One solution would be to set the PYTHONIOENCODING environment variable to UTF-8 when you call Python:
PYTHONIOENCODING=utf-8 python myscript.py
If you are using a unix-like operating system you could change your locale from a gbk locale to a utf-8 locale, for example from zh_CN.gbk
to zh_CN.utf8
(this will affect how all programs read and write from files, so this may not be a good idea if you have a lot of gbk-encoded data).
If you are using Windows, see the answers to this question for information about working with unicode in the Windows terminal.