update:encoding convert

This commit is contained in:
lan
2023-01-07 12:09:25 +08:00
parent 46b8177095
commit fd26bace79
7 changed files with 24 additions and 87 deletions
+22
View File
@@ -0,0 +1,22 @@
# 获取文件编码
import chardet
def get_encoding(file):
with open(file, 'rb') as f:
return chardet.detect(f.read())['encoding']
# 将其他编码的文件转换为UTF-8编码
def other2utf8(file, encoding):
with open(file, 'r', encoding=encoding) as f:
lines = f.readlines()
with open(file, 'w', encoding='utf-8') as f:
f.writelines(lines)
# 获取文件编码,如果为utf-8编码,就转换为gbk编码
def convert_encoding(file):
encoding = get_encoding(file)
if encoding != 'utf-8':
other2utf8(file, encoding)