字符串编码常用类型:utf-8,gb2312,cp936,gbk等。

python中,我们使用decode()和encode()来进行解码和编码

在python中,使用unicode类型作为编码的基础类型。即

     decode              encode

str ---------> unicode --------->str

 

字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(encode)成另一种编码。 

decode的作用是将其他编码的字符串转换成unicode编码,如str1.decode('gb2312'),表示将gb2312编码的字符串str1转换成unicode编码。 

encode的作用是将unicode编码转换成其他编码的字符串,如str2.encode('gb2312'),表示将unicode编码的字符串str2转换成gb2312编码。

 

假设我们现在读取的是SIMPLIFIED CHINESE_CHINA.ZHS16GBK的oracle数据库,

这时读出来的中文,需要转成UTF-8

print row[1].decode('gbk').encode('utf-8')

posted on 2014-12-25 11:26  凌度  阅读(7935)  评论(0编辑  收藏  举报