UnicodeEncodeError: 'gbk' codec can't encode character '\ue13b' in position 25: illegal multibyte sequence

折月煮酒 提交于 2021-02-08 05:35:55

问题


Error:

 UnicodeEncodeError: 'gbk' codec can't encode character '\ue13b' in position 25: illegal multibyte sequence

The file encoding format is utf-8, and there is an unrecognized word in the file when it is read. ‘左足趾麻木’

Code:

for line in open(label_filepath, encoding='utf-8'):
    print(line)

回答1:


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.




回答2:


Change the native encoding to utf-8

import sys
import io
sys.stdout = io.TextIOWrapper(buffer=sys.stdout.buffer,encoding='utf8')



回答3:


If it's caused by the editor you are using, for example. Try to add a line to the file "Python.sublime-build". It worked on mine.

reference



来源:https://stackoverflow.com/questions/55774642/unicodeencodeerror-gbk-codec-cant-encode-character-ue13b-in-position-25

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!