9Python Full Stack Road series of win character coding deep solution

Source: Internet
Author: User

Win DOS window output in Chinese

Python2.7The 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

UsePycharmOrsublimeOfPython IDEOutput 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

    • Using the Sublime IDE output

# _*_ 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 canasciiThe code is also replacedUTF-8Then surely it can be converted intoGBKCoding, 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

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.