Python--unicodeencodeerror: ' ASCII ' codec can ' t Encode/decode Characters__python

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.