- 安装
chardet
pip install chardet
- 获得文本文档编码类型
import chardet
from chardet.universaldetector import UniversalDetector
def GetEncoding(file_path):
txt = open(file_path, "rb")
detector = UniversalDetector()
for line in txt.readlines():
detector.feed(line)
if detector.done:
break
detector.close()
txt.close()
return detector.result
my_path = '.\\my_test.txt'
f = open(my_path, 'rb')
str1 = f.read()
char_encoding= chardet.detect(str1)
print(f'字符串为:{str1}')
print(f'字符串编码信息为:{char_encoding}' )
print(f'字符串编码为: {char_encoding["encoding"]}')
标签:编码,encoding,文档,path,chardet,detector,txt
From: https://www.cnblogs.com/xiacuncun/p/18190092