python字符编码检测——chardet


分类: 我的python新生 2012-04-01 15:36 21人阅读 评论(0) 收藏 举报
                当面对一串不知道编码信息的字节流的时候,尝试着确定一种编码方式以使我们能够读懂其中的文本内容。类似于破解密码。
                Firefox包含有一个自动检测字符编码的库,导入到了python2,并且取绰号为chardet模块
                chardet运用了一种算法来检测字符的类型
[python] view plaincopy
import chardet  
utf8_str = '我爱python'  
gbk_str = utf8_str.decode('UTF-8').encode('GBK')  
  
print utf8_str,chardet.detect(utf8_str)  
print gbk_str,chardet.detect(gbk_str)  


输出:
[python] view plaincopy
我爱python {'confidence': 0.7525, 'encoding': 'utf-8'}  
我爱python {'confidence': 0.99, 'encoding': 'GB2312'}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值