美文网首页
Python chardet检查文件的编码

Python chardet检查文件的编码

作者: 京樂春水 | 来源:发表于2020-04-11 15:53 被阅读0次

检查文件的编码
代码如下:

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': ''}

参考:
https://www.cnblogs.com/amou/p/9142896.html

相关文章

网友评论

      本文标题:Python chardet检查文件的编码

      本文链接:https://www.haomeiwen.com/subject/hhrnuhtx.html