Python 字符编码判断
2016-02-22 17:31
609 查看
转自:/article/4868125.html
法一:
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'}
>>>chardet可以直接用detect函数来检测所给字符的编码。函数返回值为字典,有2个元数,一个是检测的可信度,另外一个就是检测到的编码。
chardet 安装
下载chardet后,解压chardet压缩包,直接将chardet文件夹放在应用程序目录下,就可以使用import chardet开始使用chardet了。
或者使用setup.py安装文件,将chardet拷贝到Python系统目录下,这样你所有的python程序只要用import chardet就可以了。
python setup.py install参考
chardet官网 http://chardet.feedparser.org/
chardet下载页面:http://chardet.feedparser.org/download/
from:http://www.pythonclub.org/modules/chardet/article/4868125.html
转自:/article/4868125.html
法一:
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'}
>>>chardet可以直接用detect函数来检测所给字符的编码。函数返回值为字典,有2个元数,一个是检测的可信度,另外一个就是检测到的编码。
chardet 安装
下载chardet后,解压chardet压缩包,直接将chardet文件夹放在应用程序目录下,就可以使用import chardet开始使用chardet了。
或者使用setup.py安装文件,将chardet拷贝到Python系统目录下,这样你所有的python程序只要用import chardet就可以了。
python setup.py install参考
chardet官网 http://chardet.feedparser.org/
chardet下载页面:http://chardet.feedparser.org/download/
from:http://www.pythonclub.org/modules/chardet/article/4868125.html
相关文章推荐
- Python多版本共存之pyenv
- Python中*args 和**kwargs的用法
- libsvm+python配置
- Python2.7 with MySQL Connector/Python Sample(Windows OS)
- Python——运算符重载(1)
- python生成二维码
- python--knn算法练习
- pythonMD5加密
- Python java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory解决办法
- python+mysql抓取百度新闻的标题存到数据库
- python随机验证码函数
- Python
- python操作excel文件的方法
- 【python】编程语言入门经典100例--10
- python高级语法
- python zipfile模块测试
- 【python】编程语言入门经典100例--9
- Python 调用 Stanford Parser 两种方法
- An Example Using boto Amazon MWS Package
- python查询数据库并发送邮件