在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(encode)成另一种编码。
decode的作用是将其他编码的字符串转换成unicode编码,
如str1.decode('gb2312'),表示将gb2312编码的字符串转换成unicode编码。
encode的作用是将unicode编码转换成其他编码的字符串,
如str2.encode('gb2312'),表示将unicode编码的字符串转换成gb2312编码。
写一个方法将字符串转化为unicode编码
def to_unicode(s):
if isinstance(s, unicode):
return s
try: u = s.decode('utf-8')
except: u = unicode(s,'mbcs')
return u