python2.x Chinese garbled problem solving method

Source: Internet
Author: User
Tags character set in python

This article mainly introduced the python2.x Chinese garbled problem solution method, this article explained the problem reason, has given the processing method and explained the code decoding some knowledge, needs the friend to be possible to refer to under

The garbled problem in Python is a big headache.

In Python3, the Chinese language is fully supported, but the relevant settings are required in python2.x to use Chinese. Otherwise there will be garbled

"Cause of the problem"

In the python2.x is mainly the problem of character encoding, processing is not good, will lead to garbled. Python's default ASCII encoding, letters, punctuation, and other characters are represented by only one byte, but for Chinese characters, one byte does not satisfy the requirement.

The code is as follows:

>>> Import Sys

>>> sys.getdefaultencoding ()

' ASCII '

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

The two-byte Chinese coding standard is: GB2312, GBK, BIG5, etc.

"Approach"

In order to include various languages in the unified character set, to meet the international exchange of information, the international development of the Unicode character set, containing all the language characters in the world, these characters have a unique encoding, through the use of Unicode character sets to meet the cross-language word processing, to avoid the emergence of garbled.

i) interactive commands: Generally do not appear garbled, do not need to do processing

II Py script file: Cross character set must be set, otherwise garbled.

First, add the following 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 a UTF-8 format!

The above sentence is just to tell the Python compiler: 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 save it as a UTF-8 format.

If it is opened with Nodepad, save as-->utf-8

If it is opened with IDLE, "Options"-> "Configure IDLE"-> "General"

The above setting, can guarantee the idle, the operation F5, can the normal output Chinese.

"Coded decoding"

Add the #-*-Coding:utf-8-*-at the beginning and save the file in UTF-8 format, still not guaranteed to output normal output in Chinese,

Different editors, such as vim,idle,eclipse, use output encodings that are inconsistent.

Therefore, the normal output of Chinese in one place is not necessarily in another place. So you must also do coding and decoding settings!

Encode: Coding

Decode: Decoding

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

So the final solution must be decoded first and then encoded in the console format: For example, cmd default is GBK way

You must use the following methods:

Correct output Result:

"Other description"

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

The code is as follows:

>>> China = ' Chinese '

>>> Print (China)

Chinese

2. In Python3, there is no need to back and forth codecs, and string objects do not have 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.