法一:
isinstance(s, str) 用来判断是否为一般字符串
isinstance(s, unicode) 用来判断是否为unicode
或
if type(str).__name__!="unicode":
str=unicode(str,"utf-8")
else:
pass
法二:
Python chardet 字符编码判断
使用 chardet 可以很方便的实现字符串/文件的编码检测。尤其是中文网页,有的页面使用GBK/GB2312,有的使用UTF8,如果你需要去爬一些页面,知道网页编码很重要的,虽然HTML页面有charset标签,但是有些时候是不对的。那么chardet就能帮我们大忙了。
chardet实例
>>> import urllib
>>> rawdata = urllib.urlopen('http://www.google.cn/').read()
>>> import chardet
>>> chardet.detect(rawdata)
{'confidence': 0.98999999999999999, 'encoding': 'GB2312'}
原文地址:http://www.cnblogs.com/dkblog/archive/2011/03/02/1980644.html
转载请注明原文地址: https://ju.6miu.com/read-11404.html