Getting Started with Python: Convert UTF-8 to GBK Encoding

1  # !/usr/bin/env python 
2  # -*- coding:utf-8 -*- 
3  # UTF-8 converted to GBK encoding 
4  # temp (temporary employee, transliteration: Taipo) 
5  # decode(coding, Transliteration: Dikoude) 
6  # encode (encoding, transliteration: Yinkoude) 
7  #The principle is to convert UTF-8 into Universal Code, and then encode the Universal Code into GBK, which is used in python 2.x 
8  """ 
9  Assignment to variable temp is equal to 'Li Jie' is UTF-8 encoding!
 10  Assignment of variable temp_unicode is equal to decoding of temp variable, the original encoding of the specified temp is UTF-8
 11  Get the temp_unicode variable and specify the encoding as gbk, what you get is the assignment of temp_gbk
 12  temp_gbk is the compiled GBK content, print(temp_gbk) is to display the previous UTF-8 encoded 'Li Jie' in gbk
 13  """ 
14 temp = 'Li Jie ' # UTF-8 
15  #Decoding , you need to specify what the original encoding is 
16 temp_unicode =temp.decode( ' utf-8 ' )
 17  #Encode with unicode 
18 temp_gbk = temp_unicode.encode ( ' gbk ' )
 19  #Then When printing, I want to display it in GBK. The terminal of windows is just the encoding of GBK, and the two match 
20  # temp_gbk is the compiled GBK content, print(temp_gbk) is displayed in the form of gbk 
21  print (temp_gbk)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324600719&siteId=291194637