兼容UTF-8编码

自动转换UTF-8编码的.env文件为GBK编码
允许用户在根目录配置.env文件
(优先级:.env大于data/.env)
This commit is contained in:
essesoul
2023-01-03 21:10:38 +08:00
parent 08c6368cc6
commit 3cbb884dda
4 changed files with 39 additions and 2 deletions
+21
View File
@@ -0,0 +1,21 @@
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)