一.确定字符串类型
import chardet str='中文~' print str(chardet.detect(str))
二.根据类型转码
str=str.decode('GB2312').encode('utf-8') #先将字符串由得到的类型转为undecode,再转为其他字符类型
三.代码前添加
# -*- coding: utf-8 -*- reload(sys) sys.setdefaultencoding('utf-8')