As a beginner python is a very large coded format, the following bugs are one of the coding problems encountered:
"BUG" Unicodeencodeerror: ' ASCII ' codec can ' t encode characters in position 0-15:ordinal not in range (128) or Unicodedecodee Rror: ' ASCII ' codec can ' t decode byte 0x?? In position 1:ordinal not in range (128)
When Python is installed, the default encoding is ASCII, and when non-ASCII code is present in the program, Python's handling often reports that Python cannot handle non-ASCII code, and it needs to set its own default encoding for Python. An encoding format that is generally set to UTF8.
Query system default encoding you can enter the following command in the interpreter: Python code >>>sys.getdefaultencoding ()
Use when setting default encoding: Python code >>>sys.setdefaultencoding (' UTF8 ')
May report attributeerror: ' Module ' object has no attribute ' setdefaultencoding ' error, execution reload (SYS), in the execution of the above command can be successfully passed.
At this point in the execution sys.getdefaultencoding () will find that the encoding has been set to UTF8, but the modified encoding in the interpreter can only guarantee that the time is valid, after rebooting the interpreter, you will find that the encoding is reset to the default ASCII, Then there is no way to modify the program or the system's default code.
There are 2 ways to set the default encoding for Python:
A solution adds the following code to the program: Python Code import sys reload (SYS) sys.setdefaultencoding (' UTF8 ')
Another scenario is to create a new sitecustomize.py under the Python lib\site-packages folder, which reads: Python code # ENCODING=UTF8 import sys reload (SYS) SY S.setdefaultencoding (' UTF8 ')
At this point, restart the Python interpreter, execute sys.getdefaultencoding (), find that the encoding has been set to UTF8, and after multiple reboots, the effect is the same because the system calls the file itself at Python startup, setting the system's default encoding, Without the need to manually add the solution code each time, is a permanent solution.
Another solution is to enforce coding as UTF8 in all areas of the program that involve coding, that is, adding code encode ("UTF8"), which is not recommended, because once you write less one place, you will cause a lot of error reporting.
"Turn" http://shirley-ren.iteye.com/blog/1018750