python2.x Chinese garbled problem solving method

Source: Internet
Author: User
The problem of garbled characters in Python is a headache.
In Python3, the Chinese is fully supported, but in python2.x you need to make the relevant settings in order to use Chinese. Otherwise there will be garbled

"Cause of the problem"

In the python2.x is mainly the problem of character encoding, processing bad words, will lead to garbled. The ASCII encoding that Python takes by default, letters, punctuation, and other characters are represented by only one byte, but for Chinese characters, a byte does not satisfy the requirement.
Copy the Code code as follows:


>>> Import Sys
>>> sys.getdefaultencoding ()
' ASCII '

In order to be able to represent all Chinese characters in the computer, the English encoding is represented by two bytes. If the Chinese encoding and the use of ASCII mixed, it will lead to decoding errors, so that garbled. and CMD under the default encoding method is: GBK, so it caused the above garbled!

The Chinese encoding standard with two bytes is: GB2312, GBK, BIG5, etc.

"Treatment Method"

In order to include different languages in the unified character set, to meet the international exchange of information, the international development of the Unicode character set, contains all the language characters in the world, these characters have a unique encoding, by using the Unicode character set can be used to meet the cross-language word processing, avoid garbled production.
i) in the interactive command: Generally do not appear garbled, do not need to do processing

II) in the Py script file: Cross character set must be set, otherwise garbled.
First, add it in the opening sentence:
Copy the Code code as follows:


# coding = Utf-8
# or
# coding = UTF-8
# or
#-*-Coding:utf-8-*-


Next you need to save the file in UTF-8 format!

The above sentence simply tells the Python compiler that the script contains non-ASCII characters and is not converted.
If you want to change the character encoding from the default ASCII to UTF-8, you need to choose Save as UTF-8 format when saving.

If you open it with Nodepad, save as-->utf-8

If Open with IDLE, "Options", "Configure IDLE", "General"

The above setting, can guarantee idle, run F5, can output Chinese normally.

"Encode and Decode"

Add #-*-Coding:utf-8-*-at the beginning and save the file as UTF-8 format, still cannot guarantee output normal output Chinese,
The output codes used by different editors, such as vim,idle,eclipse, are inconsistent.
Therefore, in one place can output the normal Chinese, in another place is not necessarily. So you have to do encoding and decoding settings!

Encode: Encoding
Decode: Decoding

You must ensure that the encoded and decoded objects are the same. For example, UTF-8 encoding, you must use UTF-8 to decode.

So the final solution, you must first decode the original way, and then re-encode in the console format: For example, cmd default is GBK mode
You must use the following method:

Correct output Result:

"Other Notes"
1. In Python3, the support for Chinese is very comprehensive, the source file is saved by default to UTF-8 encoding, so that not only in the source code can be used in Chinese, and the variable name can also be used in Chinese, for example, say:
Copy the Code code as follows:


>>> China = ' Chinese '
>>> Print (China)
Chinese


2. In Python3, there is no need to decode back and forth, and the string object has no decode and encode methods.
  • 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.