當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。