VScode 脚本实现GB2312转UTF8
- 第一步
pip install chardet,安装对应的python库 - 第二步
运行代码,python convert_encoding.py
代码如下,文件命名为convert_encoding.py
以下代码全部由chatGTP生成,从出现想法到运行代码验证效果,全过程5分钟
import os
import chardet
def detect_encoding(file_path):
with open(file_path, 'rb') as file:
raw_data = file.read()
result = chardet.detect(raw_data)
return result['encoding']
def convert_to_utf8(file_path):
with open(file_path, 'rb') as file:
raw_data = file.read()
encoding = chardet.detect(raw_data)['encoding']
if encoding == 'GB2312':
try:
text = raw_data.decode('GB2312')
with open(file_path, 'w', encoding='utf-8') as utf8_file:
utf8_file.write(text)
print(f"Converted {file_path} to UTF-8")
except Exception as e:
print(f"Error converting {file_path}: {e}")
def scan_and_convert(folder_path):
for root, dirs, files in os.walk(folder_path):
for file in files:
file_path = os.path.join(root, file)
encoding = detect_encoding(file_path)
if encoding == 'GB2312':
convert_to_utf8(file_path)
if __name__ == "__main__":
folder_path = input("Enter the folder path to scan: ")
scan_and_convert(folder_path)