Home>Article>Backend Development> Python2.7 outputs Chinese garbled characters in the windows command window

Python2.7 outputs Chinese garbled characters in the windows command window

巴扎黑
巴扎黑 Original
2017-06-23 16:01:19 3163browse

The example in this article describes the problem of garbled characters generated when executing Python files under the windows command window. For your reference:
Ps: Please correct me if there are any mistakes. Welcome to exchange and learn

# -*- coding:utf-8 -*-str = "彦雪"print str

After execution, the output result is as follows:

褰﹂洩

The garbled code results may be different from everyone else, but they are all garbled codes!!

Problem analysis

Python2 default encoding is "ascii", ascii encoding does not include Chinese characters
If there are Chinese characters in it, the Python interpreter will generally report an error.
But if UTF-8 encoding is specified, Python will no longer Error report.
"# -- coding:utf-8 --" specifies that the Python source code is encoded in UTF-8.

The default encoding of window is gbk encoding, so str must be encoded as gbk before output.
Since Python does not allow you to directly convert utf-8 to gbk, you need to convert utf-8 to unicode first and then to gbk

In-depth analysis

This method has one One disadvantage is that problems will arise when we are cross-platform, so Python provides us with a convenient solution to use unicode as output--this method does not apply to raw_input

when printing is required When outputting, Python will first call the encoding format of the character output program (command line or output function), and then encode the string into the encoding used by the character output program (so that the character output program will not appear because it does not recognize the encoding) Garbled characters), and then the character output program outputs the encoded characters to the destination.

Solution

# 方法一 中文前加u, 告诉Python解释器后面的是个unicode编码str = u"彦雪"
# 方法二 str.decode('utf-8') 以utf-8编码对字符串 str 进行解码, 获取unicodestr = "彦雪".decode('utf-8')
# 方法三 unicode(str, 'utf-8') 将字符串 str 以utf-8编码解码, 获取unicodestr = unicode('彦雪','utf-8')

raw_input displays garbled characters in the windows command window

Using raw_input requires converting Chinese to system encoding. The method is as follows

# 方法1 str.encode("gbk") 将unicode转为gbk 编码content = raw_input(u"输入内容: ".encode("gbk"))
# 方法2content = raw_input("输入内容: ".decode('utf-8').encode("gbk"))
# 方法3content = raw_input(unicode('输入内容: ','utf-8').encode("gbk"))

Although this way of writing It is very convenient, but the cross-platform effect is poor. I personally do not recommend this writing method. It is recommended to write Chinese characters and raw_input separately. Use other means to achieve the purpose of being on the same line

Extended reading

Python's coding notes# -- coding:utf-8 --

PEP 263 -- Defining Python Source Code Encodings

About Python's encoding, garbled characters and Unicode Some research

raw_input input, file reading, variable comparison and other str, unicode, utf-8 conversion issues

Exploration

Currently resigned, while waiting, Let me share with you the problems I often encountered before and discuss them with you. I hope it will be helpful to everyone. Corrections are welcome
Find yourself in sharing knowledge and enjoy the joy of programming

The above is the detailed content of Python2.7 outputs Chinese garbled characters in the windows command window. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn