Home>Article>Backend Development> Python2.7 outputs Chinese garbled characters in the windows command window
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!!
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
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.
# 方法一 中文前加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')
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
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
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!