#GBK [decode]转换为unicode编码然后通过[encode]转换成utf-8
#utf-8[decode]转换为unicode编码然后通过[encode]转换成GBK
#encode后都是字节(bytes)类型 decode转换成字符串
#decode后是unicode
q="你哈" #默认unicoude
print(q)
q_gbk=q.encode("gbk") # 默认是unicoude直接转换成gbk
q_utf8=q.encode()#默认转成utf-8
print(q_gbk)
print(q_utf8)
gbk_to_utf8=q_gbk.decode("gbk").encode("utf-8") #告诉decode我是gbk转成unicode再encode成utf-8
print("utf-8",gbk_to_utf8)
utf8_to_gbk=q_utf8.decode("utf-8").encode("gbk")#告诉decode我是utf-8转换成unicode再转换成gbk
print("gbk",utf8_to_gbk)
s="你哈"
#转换成gb2312
s_2312=s.encode("utf-8").decode("utf-8").encode("gb2312")
s_2=s.encode("gb2312")
print("gb2312",s_2312)
print("gb2",s_2)
原文:http://blog.51cto.com/12992048/2172835