需要python, pip安装 chardet, 放到graphicPacks目录下执行
```python3
import os
import chardet
def convert_to_utf8(file_path):
with open(file_path, 'rb') as file:
raw_data = file.read()
# 使用 chardet 检测文件的编码
detected_encoding = chardet.detect(raw_data)['encoding']
print(f"检测到 {file_path} 编码: {detected_encoding}")
# 如果文件编码是 GB2312,则转换为 UTF-8
if detected_encoding and detected_encoding.lower() == 'gb2312':
decoded_data = raw_data.decode(detected_encoding)
# 以 UTF-8 编码重新保存文件
with open(file_path, 'w', encoding='utf-8') as utf8_file:
utf8_file.write(decoded_data)
print(f"已将 {file_path} 从 GB2312 转换为 UTF-8")
else:
print(f"跳过 {file_path},因为编码不是 GB2312")
def convert_rules_txt_to_utf8(folder_path):
for root, _, files in os.walk(folder_path):
for file in files:
if file == 'rules.txt':
file_path = os.path.join(root, file)
convert_to_utf8(file_path)
if __name__ == "__main__":
# 获取脚本所在的文件夹路径
script_dir = os.path.dirname(os.path.abspath(__file__))
# 只处理 rules.txt 文件
convert_rules_txt_to_utf8(script_dir)
```
```python3
import os
import chardet
def convert_to_utf8(file_path):
with open(file_path, 'rb') as file:
raw_data = file.read()
# 使用 chardet 检测文件的编码
detected_encoding = chardet.detect(raw_data)['encoding']
print(f"检测到 {file_path} 编码: {detected_encoding}")
# 如果文件编码是 GB2312,则转换为 UTF-8
if detected_encoding and detected_encoding.lower() == 'gb2312':
decoded_data = raw_data.decode(detected_encoding)
# 以 UTF-8 编码重新保存文件
with open(file_path, 'w', encoding='utf-8') as utf8_file:
utf8_file.write(decoded_data)
print(f"已将 {file_path} 从 GB2312 转换为 UTF-8")
else:
print(f"跳过 {file_path},因为编码不是 GB2312")
def convert_rules_txt_to_utf8(folder_path):
for root, _, files in os.walk(folder_path):
for file in files:
if file == 'rules.txt':
file_path = os.path.join(root, file)
convert_to_utf8(file_path)
if __name__ == "__main__":
# 获取脚本所在的文件夹路径
script_dir = os.path.dirname(os.path.abspath(__file__))
# 只处理 rules.txt 文件
convert_rules_txt_to_utf8(script_dir)
```