python2 编码与解码

 1 #!coding: utf-8
 2 s  = "特斯拉"
 3 s_to_unicode = s.decode("utf-8")
 4 unicode_to_gbk = s_to_unicode.encode("gbk")
 5 print s
 6 print ("unicode:", s_to_unicode)
 7 print ("gbk", unicode_to_gbk)
 8 
 9 gbk_to_unicode = unicode_to_gbk.decode("gbk")
10 unicode_to_utf8 = gbk_to_unicode.encode("utf-8")
11 print (gbk_to_unicode)
12 print (unicode_to_utf8)
1 输出结果
2 [root@python]# python uncode.py
3 特斯拉
4 ('unicode:', u'\u7279\u65af\u62c9')
5 ('gbk', '\xcc\xd8\xcb\xb9\xc0\xad')
6 特斯拉
7 特斯拉

 

posted @ 2018-01-15 14:46  lixinliang  阅读(416)  评论(0编辑  收藏  举报