Win DOS window output in Chinese
Python2.7
The default character encoding is the ascii
format, even if the specified character encoding is UTF-8, it may not be able to output Chinese, the test is as follows:
#_ *_coding:utf-8_*_# defines a variable content in Chinese, the character set is utf-8temp = "Chinese" # Output variable temp content print (temp)
Use the DOS window of win to execute this script to see if the output is Chinese
c:\users\anshe>python f:\python_code\sublime\day02\print.py Juan Po #输出出来的是乱码
GBK (for example), the specified character set of the output must be GBK, so the output of Chinese is garbled.
650) this.width=650; "src=" Https://blog.ansheng.me/static/uploads/2016/12/1483016145.png "alt=" Python-day02-01 "style=" border:0px;vertical-align:middle; "/>
Notice I'm using python2.7.11
C:\users\anshe>python-vpython 2.7.11
UsePycharm
Orsublime
OfPython IDE
Output Chinese process
650) this.width=650; "src=" Https://blog.ansheng.me/static/uploads/2016/12/1483016187.png "alt=" python-day02-02 " Style= "Border:0px;vertical-align:middle;"/>
procedure Chinese Description:
py Scripts header Specifies the encoding format for UTF-8
-> ide encodes the default ASCII format into UTF-8
--> ide Terminal output Chinese
# _*_ Coding:utf-8 _*_# defines a variable content in Chinese, character set is utf-8temp = "Chinese" # Output variable temp content print (temp)
Content of the output:
650) this.width=650; "src=" Https://blog.ansheng.me/static/uploads/2016/12/1483016219.png "alt=" python-day02-05 " Style= "Border:0px;vertical-align:middle;"/>
In this case, Python's code canascii
The code is also replacedUTF-8
Then surely it can be converted intoGBK
Coding, the process is as follows:
650) this.width=650; "src=" Https://blog.ansheng.me/static/uploads/2016/12/1483016246.png "alt=" python-day02-03 " Style= "Border:0px;vertical-align:middle;"/>
The code is as follows:
# _*_ Coding:utf-8 _*_# defines a variable content in Chinese, the character set for utf-8temp = "Chinese" # Decoding, need to specify what the original encoding Temp_unicode = Temp.decode ("Utf-8") # Encoded, Need to specify what encoding to convert TEMP_GBK = Temp_unicode.encode ("GBK") # Output converted to GBK encoding print (TEMP_GBK)
DOS Window execution test:
C:\users\anshe>python F:\Python_code\sublime\Day02\print.py Chinese
Another method:
Code:
# _*_ coding:utf-8 _*_# defines a variable content in Chinese, the character set is utf-8temp = "Chinese" # decoding, You need to specify what encoding Temp_unicode = temp.decode ("Utf-8") # output is converted to GBK encoding print (Temp_unicode) # Windows terminal requires Gbk,dos automatically converted to GBK
C:\users\anshe>python F:\Python_code\sublime\Day02\print.py Chinese
Probably the process is just like the diagram below.
650) this.width=650; "src=" Https://blog.ansheng.me/static/uploads/2016/12/1483016284.png "alt=" python-day02-04 " Style= "Border:0px;vertical-align:middle;"/>
Python3 removes the Unicode character set type, uses UTF-8 by default, and a method that can also output Chinese in a DOS terminal, with the following code:
# _*_ Coding:utf-8 _*_print (U "Chinese")
DOS window output is Chinese, although I do not know what the meaning of
C:\users\anshe>python f:\Python_code\sublime\Day02\print.py Chinese
#Python全栈之路
Anne
This article is from "Eden" blog, declined reprint!
9Python Full Stack Road series of win character coding deep solution