- 博客(1)
- 资源 (3)
- 收藏
- 关注
原创 KEIL 中文乱码 非UTF格式文件批量修改
在用Keil 环境编写程序过程中或引用第三方程序时会出现的中文乱码现象,经查找需要.在editor的选项卡里面encoding把编码改为chinese gb2312或utf-8,但已经存在的.C .H文件编码的修改就需要采用另存方式一个一个手动修改,当项目中文件众多或多个项目时就很麻烦,于是就用了下面的代码来批量修改。3、运行(点击保存路径,输入cmd回车打开代码窗口,接着输入python A.py(注意!:param extensions: 需要处理的文件后缀列表(如 [‘.txt’, ‘.csv’])
2025-04-23 14:25:12
847
2
批量修改三种后缀为.c.h.v文件的编码模式为 utf-8编码,用于解决keil 中文乱码手动修改每个文件的麻烦
用python写的批处理程序,在跳出的弹窗选择需要批量修改的文件夹即可。注意仅在公司几台WIN10系统下测试通过。在批量测试中发现偶尔有几个文件显示转换成功但打开后发现还是原格式。源码如下:
import os
import chardet
import PySimpleGUI as sg;
def detect_file_encoding(file_path):
"""
检测文件的编码
:param file_path: 文件路径
:return: 检测到的编码
"""
with open(file_path, 'rb') as f:
raw_data = f.read()
result = chardet.detect(raw_data)
return result['encoding']
def convert_file_to_utf8(file_path):
"""
将单个文件转换为 UTF-8 编码
:param file_path: 文件路径
"""
try:
# 检测文件编码
original_encoding = detect_file_encoding(file_path)
if not original_encoding:
print(f"无法检测编码: {file_path}")
return
# 读取文件内容(使用检测到的编码)
with open(file_path, 'r', encoding=original_encoding) as f:
conte
2025-04-23
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人