python2.x 中中间编码为unicode,一个字符串需要decode为unicode,再encode为其它编码格式(gbk、utf8等)
以gbk转utf8为例:
s = "我是字符串" #gbk编码
s = s.decode('gbk').encode('utf8') #将gbk编码转换为utf8编码,需要先转换为unicode中间编码,再转换为utf8编码
具体过程为:
s | gbk --- decode('gbk') ---> s | unicode --- encode('utf8') ---> s | utf8
代码中的 显式字符串 默认编码为代码文件的编码格式,如果开头没有#coding=[]声明,则默认编码为ascii型(所以存储中文会有问题)