1. In Python2:
#-*-coding:utf-8-*-import Sysprint (sys.getdefaultencoding ()) #获取系统默认编码 #1.utf-8 Turn gbks = "Hello" S_to_unicode = S.D Ecode ("Utf-8") #将utf-8 type Convert to Unicodeprint (s_to_unicode) print (Type (s_to_unicode)) S_TO_GBK = S.decode ("Utf-8"). Enco De ("GBK") #utf-8 to GBK: Decode Utf-8 first into Unicode, encode into Gbkprint (S_TO_GBK) #注: Unicode can print utf-8 encoded characters directly #2.gbk to Utf-8gbk_ To_utf8 = S_to_gbk.decode ("GBK"). Encode ("Utf-8") print (Gbk_to_utf8)
2. In Python:
Import Sysprint (sys.getdefaultencoding ()) #获取系统默认编码utf-8 (Ignore) msg = "Hello" #默认就是unicode, no more Decodeprint (msg) msg_gb2312 = Msg.encode ("gb2312") #unicode转成gb2312, turn gb2312 to bytes type Prin T (msg_gb2312) Gb2312_to_unicode = Msg_gb2312.decode ("gb2312") #gb2312转unicodeprint (Gb2312_to_unicode) gb2312_to_ UTF8 = Msg_gb2312.decode ("gb2312"). Encode ("Utf-8") #gb2312转utf-8, turn into utf-8 and convert to bytes type print (GB2312_TO_UTF8)
Python---character encoding and transcoding