In Python, the processing of Chinese characters has always been a headache, a word is garbled, and the cause of garbled is not the same, sometimes Python itself default encoder settings are not correct, and sometimes the use of the IDE decoder is not correct, There are sometimes terminal terminal decoder is not correct, the same code in the Python2 on the normal operation, Python3 on it, anyway, there are many reasons for garbled, here are some of the bloggers have encountered some errors and solutions:
Error 1:
Unicodeencodeerror: ' GBK ' codec can ' t encode character ' \x?? ' in position?: illegal multibyte sequence
This is in the Win7 with Python3 compile time encountered, because Win7 python3 default encoding is not UTF8, we just need to change the default encoding to UTF8 to solve the problem, see the following code:
Import= io. Textiowrapper (sys.stdout.buffer,encoding='UTF8'# Change default encoding To UTF8
Error 2:
Unicodedecodeerror: ' ASCII ' codec can ' t decode byte 0x?? In Position:ordinal not in range (128)
This is the Mac on the use of Python2 compile time encountered, because it is also because the default encoding python2 is not UTF8, we only need to change the default encoding UTF8 to solve the problem, see the following code:
Import sysreload (SYS) sys.setdefaultencoding ('utf-8')
Like other causes of errors, such as the IDE display garbled, or terminal terminal display garbled, you can quickly find a solution by Google search, here will not repeat.
Resources:
Http://ajucs.com/2015/11/10/Python-character-encoding-explained.html
Python Solve unicodeencodeerror ' gbk '/' ASCII '/' UTF8 ' codec can ' t encode character ' \x?? ' in position? Troubleshoot Python-encoded errors