# coding=utf-8
import os
import chardet
root = os.getcwd()
path = os.path.sep.join((root, "lst.txt"))
with open(path, "rb") as f:
data = f.read()
en = chardet.detect(data)
print(data.decode(en["encoding"]))
判断大文件编码格式,参考:https://www.cnblogs.com/Neeo/articles/11528011.html:
大文件编码判断
上面的例子,是一下子读完,然后进行判断,但这不适合大文件。
因此,这里我们选择对读取的数据进行分块迭代,每次迭代出的数据喂给detector,当喂给detector数据达到一定程度足以进行高准确性判断时,detector.done
返回True
。此时我们就可以获取该文件的编码格式。
import requests
from chardet.universaldetector import UniversalDetector
url = 'https://chardet.readthedocs.io/en/latest/index.html'
response = requests.get(url=url, stream=True)
detector = UniversalDetector()
for line in response.iter_lines():
detector.feed(line)
if detector.done:
break
detector.close()
print(detector.result) # {'encoding': 'utf-8', 'confidence': 0.99, 'language': ''}