检查文件的编码
代码如下:
from chardet.universaldetector import UniversalDetector
with open("xxxx.cfg", 'rb') as fr:
detector = UniversalDetector()
for line in fr.readlines():
print(line)
detector.feed(line)
if detector.done:
break
# 必须要有,否则检测不出结果
detector.close()
print(detector.result)
结果如下:
{'encoding': 'ascii', 'confidence': 1.0, 'language': ''}
网友评论