Convert from ASCII to Utf-8

Source: Internet
Author: User

When Python is installed, the default encoding is ASCII, and when non-ASCII encoding occurs in the program, Python processing often reports such errors:

Unicodedecodeerror: ' ASCII ' codec can ' t decode byte 0x?? In position 1:ordinal not in range (128)

Python has no way of handling non-ASCII encoding, it needs to change Python's encoding as ' utf-8 '

The first method:

At the beginning of the xx.py, add:

Import sysreload (SYS) sys.setdefaultencoding (' uft-8 ')

The downside is that this code needs to be added to every non-ASCII encoded program.

The second method:

Create a new sitecustomize.py in the Python lib\site-packages folder, with the following content:

# Encoding:utf-8import sysreload (SYS) sys.setdefaultencoding (' uft-8 ')

Restart the Python interpreter, execute sys.getdefaultencoding (), encode is set to UTF8,

Each time the Python is started, the encoding is utf-8. This is because the system calls the file on its own when the python is started, and sets the system's default encoding without having to manually add the solution code each time.

 

Convert from ASCII to Utf-8

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.