python 字符集转换
decode的作用是将其他编码的字符串转换成unicode编码,如str1.decode('gb2312'),表示将gb2312编码的字符串转换成unicode编码。
encode的作用是将unicode编码转换成其他编码的字符串,如str2.encode('gb2312'),表示将unicode编码的字符串转换成gb2312编码。
to unicode:
s = '中文'
此时s的字符集与环境有关,比如在utf-8环境下,转成unicode需要:
s.decode('utf-8')
from unicode
s = u'中文'
s.encode('utf-8')
reference: