Getting started with Python: UTF-8 conversion to GBK encoding

Source: Internet
Author: User

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 #UTF-8 converted to GBK encoding4 #Temp (Temporary employee, transliteration: Thai slope)5 #Decode (code, transliteration: Dikod)6 #Encode (code, transliteration: Because of the mouth de)7 #the principle is that Dad UTF-8 converted to the universal code, and then to the code to convert to GBK, in Python 2.x8 """9 Assigning a value to the variable temp equals ' Li Jie ' is UTF-8 encoded! Ten the assignment of the variable Temp_unicode equals the decoding of the TEMP variable, and the specified temp original encoding is UTF-8 One get the Temp_unicode variable, the encoding is specified as GBK, get the TEMP_GBK assignment A TEMP_GBK is the GBK content, print (TEMP_GBK) is displayed in GBK way before UTF-8 encoded ' Li Jie ' - """ -temp ='Li Jie'#UTF-8 the #decoding, you need to specify what the original code -Temp_unicode =temp.decode ('Utf-8') - #encode with Unicode -TEMP_GBK = Temp_unicode.encode ('GBK') + #when printing again want to show in a GBK way, the Windows terminal is just GBK encoding, both match - #TEMP_GBK is the GBK content, print (TEMP_GBK) is displayed in a GBK way. + Print(TEMP_GBK)

1 #/usr/bin/env python2 #-*-coding:utf-8-*-3 #Py24 """5 Windows terminal needs GBK, just give him Unicode encoding, it can be automatically converted to the appropriate encoding type6 so with TEMP_GBK code for GBK that step can actually not write, Windows terminal will automatically encode! 7 know on the line, write the time to write the complete, because there is no Unicode type in the Py3, can not make it!8 """9temp ="Li Jie" #Utf-8Ten #decoding, you need to specify what the original code OneTemp_unicode =temp.decode ('Utf-8') A Print(Temp_unicode) - #TEMP_GBK = Temp_unicode.encode ("GBK") - #print (TEMP_GBK)

Getting started with Python: UTF-8 conversion to GBK encoding

Related Article

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.