python2 编码与解码

时间:2023-12-24 11:37:55
 #!coding: utf-8
s = "特斯拉"
s_to_unicode = s.decode("utf-8")
unicode_to_gbk = s_to_unicode.encode("gbk")
print s
print ("unicode:", s_to_unicode)
print ("gbk", unicode_to_gbk) gbk_to_unicode = unicode_to_gbk.decode("gbk")
unicode_to_utf8 = gbk_to_unicode.encode("utf-8")
print (gbk_to_unicode)
print (unicode_to_utf8)
 输出结果
[root@python]# python uncode.py
特斯拉
('unicode:', u'\u7279\u65af\u62c9')
('gbk', '\xcc\xd8\xcb\xb9\xc0\xad')
特斯拉
特斯拉

python2 编码与解码