字符串編碼常用類型: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')