Files
filecodebox/utf8togbk.py
T
essesoul 3cbb884dda 兼容UTF-8编码
自动转换UTF-8编码的.env文件为GBK编码
允许用户在根目录配置.env文件
(优先级:.env大于data/.env)
2023-01-03 21:10:38 +08:00

22 lines
527 B
Python

import chardet
# 将UTF-8编码的文件转换为GBK编码
def utf8togbk(file):
with open(file, 'r', encoding='utf-8') as f:
lines = f.readlines()
with open(file, 'w', encoding='gbk') as f:
f.writelines(lines)
# 获取文件编码
def get_encoding(file):
with open(file, 'rb') as f:
return chardet.detect(f.read())['encoding']
# 获取文件编码,如果为utf-8编码,就转换为gbk编码
def convert_encoding(file):
if get_encoding(file) == 'utf-8':
utf8togbk(file)