当前位置: 首页>>代码示例>>Python>>正文


Python chardet.UniversalDetector方法代码示例

本文整理汇总了Python中chardet.UniversalDetector方法的典型用法代码示例。如果您正苦于以下问题:Python chardet.UniversalDetector方法的具体用法?Python chardet.UniversalDetector怎么用?Python chardet.UniversalDetector使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在chardet的用法示例。


在下文中一共展示了chardet.UniversalDetector方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: import chardet [as 别名]
# 或者: from chardet import UniversalDetector [as 别名]
def __init__(self, fobj, buffer_size=io.DEFAULT_BUFFER_SIZE, default_encoding='utf-8'):
        buffer = b''
        detector = UniversalDetector()

        while True:
            read = fobj.read(buffer_size)
            detector.feed(read)
            buffer += read
            if detector.done or len(read) < buffer_size:
                break

        if detector.done:
            self.encoding = detector.result['encoding']
        else:
            self.encoding = default_encoding

        self._fobj = fobj
        self._buffer = buffer 
开发者ID:doccano,项目名称:doccano,代码行数:20,代码来源:utils.py

示例2: get_encoding

# 需要导入模块: import chardet [as 别名]
# 或者: from chardet import UniversalDetector [as 别名]
def get_encoding(form):
    detector = UniversalDetector()
    for chunk in form.cleaned_data["upload_file"].chunks():
        detector.feed(chunk)
        if detector.done:
            break
    detector.close()
    return detector.result["encoding"] 
开发者ID:byro,项目名称:byro,代码行数:10,代码来源:members.py

示例3: detect_list_encoding

# 需要导入模块: import chardet [as 别名]
# 或者: from chardet import UniversalDetector [as 别名]
def detect_list_encoding(self, items, default=DEFAULT_ENCODING):
        detector = chardet.UniversalDetector()
        for text in items:
            if not isinstance(text, bytes):
                continue
            detector.feed(text)
            if detector.done:
                break

        detector.close()
        return normalize_result(detector.result, default) 
开发者ID:occrp-attic,项目名称:ingestors,代码行数:13,代码来源:encoding.py

示例4: guess_file_encoding

# 需要导入模块: import chardet [as 别名]
# 或者: from chardet import UniversalDetector [as 别名]
def guess_file_encoding(fh, default=DEFAULT_ENCODING):
    """Guess encoding from a file handle."""
    start = fh.tell()
    detector = chardet.UniversalDetector()
    while True:
        data = fh.read(1024 * 10)
        if not data:
            detector.close()
            break
        detector.feed(data)
        if detector.done:
            break

    fh.seek(start)
    return normalize_result(detector.result, default=default) 
开发者ID:bkerler,项目名称:android_universal,代码行数:17,代码来源:encoding.py

示例5: get_encoding

# 需要导入模块: import chardet [as 别名]
# 或者: from chardet import UniversalDetector [as 别名]
def get_encoding(filename):
    """Get the encoding of the file

    This function uses the chardet package for detecting the encoding of a 
    file.

    Parameters
    ----------
    filename: str
        Path to a file

    Returns
    -------
    encoding: str
        Encoding of the file.
    """
    detector = chardet.UniversalDetector()
    final_chunk = False
    blk_size = 65536
    with open(filename, "rb") as fid:
        while (not final_chunk) and (not detector.done):
            chunk = fid.read(blk_size)
            if len(chunk) < blk_size:
                final_chunk = True
            detector.feed(chunk)
    detector.close()
    encoding = detector.result.get("encoding", None)
    return encoding 
开发者ID:alan-turing-institute,项目名称:CleverCSV,代码行数:30,代码来源:utils.py

示例6: get_encoding

# 需要导入模块: import chardet [as 别名]
# 或者: from chardet import UniversalDetector [as 别名]
def get_encoding(filename):
    detector = chardet.UniversalDetector()
    final_chunk = False
    blk_size = 65536
    with open(filename, "rb") as fid:
        while (not final_chunk) and (not detector.done):
            chunk = fid.read(blk_size)
            if len(chunk) < blk_size:
                final_chunk = True
            detector.feed(chunk)
    detector.close()
    encoding = detector.result.get("encoding", None)
    return encoding 
开发者ID:alan-turing-institute,项目名称:CleverCSV,代码行数:15,代码来源:human.py


注:本文中的chardet.UniversalDetector方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。