美文网首页
01-chardet编码检测

01-chardet编码检测

作者: longgb246 | 来源:发表于2016-10-29 16:06 被阅读0次

一、基本用法

使用detect函数

输入字符串,输出检测的编码和置信度。

import urllib
rawdata = urllib.urlopen('http://yahoo.co.jp/').read()
import chardet
chardet.detect(rawdata)

[out] {'encoding': 'EUC-JP', 'confidence': 0.99}

二、高级用法

处理大量文本,增量式的检测。

import urllib
from chardet.universaldetector import UniversalDetector

usock = urllib.urlopen('http://yahoo.co.jp/')
detector = UniversalDetector()
for line in usock.readlines():
 detector.feed(line)
 if detector.done: break
detector.close()
usock.close()
print detector.result

[out] {'encoding': 'EUC-JP', 'confidence': 0.99}

使用UniversalDetector()检测器,.feed()添加检测文本,增量检测的时候,如果达到最小阈值,则.done的值为True
使用.close()关闭,.result为结果。

import glob
from chardet.universaldetector import UniversalDetector

detector = UniversalDetector()
for filename in glob.glob('*.xml'):
    print filename.ljust(60),
    detector.reset()
    for line in file(filename, 'rb'):
        detector.feed(line)
        if detector.done: break
    detector.close()
    print detector.result

.reset()UniversalDetector()检测器的重用。

相关文章

  • 01-chardet编码检测

    一、基本用法 使用detect函数 输入字符串,输出检测的编码和置信度。 二、高级用法 处理大量文本,增量式的检测...

  • Python-chardet 编码检测

    1.使用chardet 当我们拿到一个bytes时,就可以对其检测编码。用chardet检测编码,只需要一行代码:...

  • chardet

    安装chardet 使用chardet 检测出的编码是ascii,注意到还有个confidence字段,表示检测的...

  • Python-编码检测

    输出:

  • java部署ubuntu后中文显示问号问题

    1、首先先回忆自身项目的编码格式,即在本地进行编码时使用的编码格式。UTF-82、检测tomcat的设置问题,在w...

  • python 包集合

    Python常用库 Chardet字符编码探测器,可以自动检测文本、网页、xml的编码。 colorama主要用来...

  • 【收藏稳赚】Python常用的1000+库大盘点

    Python常用库 Chardet字符编码探测器,可以自动检测文本、网页、xml的编码。 colorama主要用来...

  • chrome浏览器使用

    设置编码 “自定义及控制”--“更多工具”--“编码”:选择unicode(utf-8),及自动检测 查看源代码及...

  • Python库集合完毕

    python库的字典诞生了 Chardet字符编码探测器,可以自动检测文本、网页、xml的编码。 colorama...

  • 终于把所有的Python库,都整理出来啦!

    来源丨网络 库名称简介 Chardet字符编码探测器,可以自动检测文本、网页、xml的编码。 colorama主要...

网友评论

      本文标题:01-chardet编码检测

      本文链接:https://www.haomeiwen.com/subject/jhdyuttx.html