Python——UnicodeEncodeError: 'ascii' codec can't encode/decode characters

Beginner Python is very troubled by the encoding format. The following bug is one of the encoding problems encountered:

【BUG】UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-15: ordinal not in range(128)或者UnicodeDecodeError: 'ascii' codec can't decode byte 0x?? in position 1: ordinal not in range(128)

When python is installed, the default encoding is ascii. When non-ascii encoding appears in the program, python processing will often report such an error. Python cannot handle non-ascii encoding. At this time, you need to set the default encoding of python by yourself, generally set to utf8 encoding format.

To query the default encoding of the system, enter the following command in the interpreter:

Python code   favorite code
  1. >>>sys.getdefaultencoding()  

When setting the default encoding use:

Python code   favorite code
  1. >>>sys.setdefaultencoding('utf8')  

 An error of AttributeError: 'module' object has no attribute 'setdefaultencoding' may be reported, execute reload(sys), and the above command can be successfully passed.

At this time, when executing sys.getdefaultencoding(), you will find that the encoding has been set to utf8, but the encoding modified in the interpreter can only be guaranteed to be valid for the current time. After restarting the interpreter, you will find that the encoding has been reset to the default ascii. So is there a way to modify the default encoding of the program or system at one time?

 

There are 2 ways to set python's default encoding:

A solution is to add the following code to the program:

Python code   favorite code
  1. import sys  
  2. reload(sys)  
  3. sys.setdefaultencoding('utf8')   

 Another solution is to create a new sitecustomize.py under the Lib\site-packages folder of python, the content is:

Python code   favorite code
  1. # encoding=utf8  
  2. import sys  
  3.   
  4. reload(sys)  
  5. sys.setdefaultencoding('utf8')   

At this time, restart the python interpreter, execute sys.getdefaultencoding(), and find that the encoding has been set to utf8. After multiple restarts, the effect is the same. This is because the system calls this file by itself when python starts, and sets the default encoding of the system. It does not need to manually add the solution code every time, which is a once-and-for-all solution.

 

Another solution is to force the encoding to utf8 in all places involving encoding in the program, that is, add the code encode("utf8"). This method is not recommended, because once one less place is written, it will lead to a large number of error reports.

【转】http://shirley-ren.iteye.com/blog/1018750

Guess you like

Origin blog.csdn.net/args_/article/details/52668797