本文整理汇总了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
示例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"]
示例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)
示例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)
示例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
示例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