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