Python2.x Chinese garbled Problem Solution

Source: Internet
Author: User

Python2.x Chinese garbled Problem Solution

This article mainly introduces how to solve Python Chinese garbled characters. This article explains the cause of the problem, provides the solution, and explains some knowledge about encoding and decoding. For more information, see

Garbled characters in Python are a headache.

In Python3, the Chinese language is fully supported. However, in Python2.x, you need to set the Chinese language. Otherwise, garbled characters will appear.

[Cause]

In Python2.x, it is mainly about character encoding. Otherwise, garbled characters may occur. Python uses ASCII encoding by default. Letters, punctuation marks, and other characters are represented in only one byte, but one byte cannot meet the requirements of Chinese characters.

The Code is as follows:

>>> Import sys

>>> Sys. getdefaultencoding ()

'Ascii'

To represent all Chinese characters in a computer, the Chinese encoding uses two bytes. If the Chinese encoding and ASCII are used in combination, decoding errors may occur, leading to garbled characters. The default encoding method in CMD is GBK, which causes the above garbled characters!

The two-byte Chinese encoding standards are: GB2312, GBK, and BIG5.

[Solution]

To include different languages in a unified character set for international information exchange, the UNICODE character set has been developed internationally, including all the characters in the world, these characters are uniquely encoded. The UNICODE character set can be used for cross-language text processing to avoid garbled characters.

I) In interactive commands: Generally, no Garbled text is required.

Ii) In The py script file: the cross-character set must be set, otherwise garbled.

Add the following In the first sentence:

The Code is as follows:

# Coding = UTF-8

# Or

# Coding = UTF-8

# Or

#-*-Coding: UTF-8 -*-

Second, you need to save the file as the UTF-8 format!

The above sentence only tells the Python compiler that the script contains non-ASCII characters without conversion.

If you want to change the character encoding from the default ascii to the UTF-8, you need to choose Save As UTF-8 format when saving.

If you open it with NODEPAD, [Save As] --> UTF-8

If you use IDLE to open it, choose Options> Configure IDLE> General]

The above settings can ensure IDLE, run F5, and output Chinese characters normally.

[Encoding and decoding]

Added #-*-coding: UTF-8-*-at the beginning and saved the file in UTF-8 format, still cannot ensure that the normal output of Chinese,

Different editors, such as VIM, IDLE, and Eclipse, use different output codes.

Therefore, Chinese characters can be normally output in one place, but not necessarily in another place. Therefore, encoding and decoding settings are required!

Encode: Encoding

Decode: decode

The encoding and decoding objects must be the same. For example, UTF-8 encoding, must be decoded with a UTF-8.

Therefore, the final solution must be decoded in the original mode and re-encoded in the console format. For example, CMD uses the GBK mode by default.

You must use the following method:

Correct output:

[Other description]

1. in Python3, the support for Chinese is very comprehensive, the source file is saved as the UTF-8 encoding by default, so that not only can use Chinese in the source code, but also the variable name can also use Chinese, for example:

The Code is as follows:

>>> China = 'China'

>>> Print (China)

Chinese

2. In Python3, no back-and-forth encoding/decoding is required, and the string object does not have the decode and encode methods.

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.