Python coding problem: ' ASCII ' codec can ' t encode characters in position solution

Source: Internet
Author: User

Error:

' ASCII ' codec can ' t encode characters in position 8-50:ordinal not in range (128)

When Python is installed, the default encoding is ASCII, and when non-ASCII encoding occurs in the program, Python processing often reports such an error unicodedecodeerror: ' ASCII ' codec can ' t decode byte 0x?? In position 1:ordinal No in range, Python does not handle non-ASCII encoding, it is necessary to set its own default encoding python, generally set to UTF8 encoding format.

Query system default encoding you can enter the following command in the interpreter:

Python Code

    1. >>>sys.getdefaultencoding ()

To set the default encoding, use:

Python Code

    1. >>>sys.setdefaultencoding (' UTF8 ')

May be reported attributeerror: ' Module ' object has no attribute ' setdefaultencoding ' error, execution reload (SYS), in the execution of the above command can be passed smoothly.

At this point, the execution of sys.getdefaultencoding () will find that the encoding has been set to UTF8, but the code modified in the interpreter can only be guaranteed to be valid, after restarting the interpreter, it will be found that the encoding is reset to the default ASCII, Then there is no way to modify the program or the system's default code at once.

There are 2 ways to set the default encoding for Python:

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

< Span style= "FONT-SIZE:14PX; Color: #454545; Font-family:tahoma, Helvetica, Arial, Stheiti; line-height:21px ">python code   &NBSP;&NBSP;

    1. Import Sys
    2. Reload (SYS)
    3. Sys.setdefaultencoding (' UTF8 ')

Another option is to create a new sitecustomize.py under the Python lib\site-packages folder, with the following:

< Span style= "FONT-SIZE:14PX; Color: #454545; Font-family:tahoma, Helvetica, Arial, Stheiti; line-height:21px ">python code   &NBSP;&NBSP;

    1. # Encoding=utf8
    2. Import Sys
    3. Reload (SYS)
    4. Sys.setdefaultencoding (' UTF8 ')

Now restart the Python interpreter, execute sys.getdefaultencoding (), found that the encoding has been set to UTF8, after multiple restarts, the effect is the same, because the system when the Python boot, the file itself, set the system's default encoding, Without the need to manually add a solution to the code each time, is a solution.

There is also a solution in the program all involved in coding, Force code to UTF8, that is, add code encode ("UTF8"), this method is not recommended, because once less write a place, will lead to a lot of error reporting, I have encountered this situation, Error log compression is still more than 70 k, all this is a problem, people have a very bad feeling.

Python coding problem: ' ASCII ' codec can ' t encode characters in position solution

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.