Python sets the default encoding to utf8, Which is pythonutf8.

Source: Internet
Author: User

Python sets the default encoding to utf8, Which is pythonutf8.

This example describes how to set the default encoding to utf8 in Python. We will share this with you for your reference. The details are as follows:

This is a Python encoding problem. Set the default python encoding to utf8.

Python installation directory:/etc/python2.x/sitecustomize. py

Import sysreload (sys) sys. setdefaultencoding ('utf-8') try: import apport_python_hookhandle t ImportError: passelse: apport_python_hook.install ()

In windows:

You can create a new sitecustomize In the Lib/site-packages directory under the Python installation directory. py file (you can also create it elsewhere, and then manually import it here. The setting will automatically take effect each time you start Python). The content is as follows:

Import syssys. setdefaultencoding ('utf-8') # set default encoding to UTF-8

Then you can see that the change has taken effect.

>>> Import sys >>> sys. getdefaultencoding () 'utf-8'

At this time, run the program. If the previous error is still reported, you only need to set the output encoding explicitly.

Print s. encode ('utf-8 ')

You can see the correct display.

Related Article

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.