import chardet
# 检测文件编码
def detect_encoding(filename):
with open(filename, 'rb') as f:
result = chardet.detect(f.read())
return result['encoding']
# 使用检测到的编码读取文件
def read_file(filename):
encoding = detect_encoding(filename)
with open(filename, 'r', encoding=encoding) as f:
content = f.read()
return content
# 使用示例
content = read_file('yourfile.txt')
print(content)
import chardet
from config.conf import cm
# 使用 Python 的 chardet 库来检测编码。
with open(cm.REPORT_FILE, 'rb') as f:
result = chardet.detect(f.read())
print(result['encoding'])
with open('yourfile.txt', 'rb') as f:
result = chardet.detect(f.read())
print(result['encoding'])
with open(cm.REPORT_FILE, encoding='GB2312') as f:
content_html = f.read()
Python文件编码检测实战
835

被折叠的 条评论
为什么被折叠?



