批量转换文件编码gb2312转utf-8
批量转换文件编码gb2312转utf-8
锟斤拷烫烫烫
这几天网上找代码学习用vscode打开之后中文全是乱码,转换编码为 gb2312
后才正常显示,之前也因为这玩意狠狠的踩坑( Keil
默认的编码对中文来说太坑了。但是一个工程里文件很多,所以就需要一个批量把这些文件转换为 utf-8
的脚本。

参考博客
关于文件编码
b站上找到的这个视频挺好。
「烫烫屯屯锟斤拷」揭秘ASCII、GBK、UTF-8,B站独家,一听就懂
脚本
其实网上随便一搜就有好多,试了一个效果不错,但是只能转换根目录下的文件。于是我稍微做了点修改。
效果还是不错的
运行结果
请输入要转换编码的文件夹路径:C:\Demo_OLED_STM32F103RCT6_Software_IIC
===============================================================
fileName fileEncoding
===============================================================
core_cm3.c GB18030
core_cm3.h GB18030
iic.c GB18030
iic.h GB18030
bmp.h GB18030
oled.c GB18030
oled.h GB18030
oledfont.h GB18030
RTE_Components.h GB18030
misc.h GB18030
stm32f10x_adc.h GB18030
stm32f10x_bkp.h GB18030
stm32f10x_can.h GB18030
stm32f10x_cec.h GB18030
stm32f10x_crc.h GB18030
stm32f10x_dac.h GB18030
stm32f10x_dbgmcu.h GB18030
stm32f10x_dma.h GB18030
stm32f10x_exti.h GB18030
stm32f10x_flash.h GB18030
stm32f10x_fsmc.h GB18030
stm32f10x_gpio.h GB18030
stm32f10x_i2c.h GB18030
stm32f10x_iwdg.h GB18030
stm32f10x_pwr.h GB18030
stm32f10x_rcc.h GB18030
stm32f10x_rtc.h GB18030
stm32f10x_sdio.h GB18030
stm32f10x_spi.h GB18030
stm32f10x_tim.h GB18030
stm32f10x_usart.h GB18030
stm32f10x_wwdg.h GB18030
misc.c GB18030
stm32f10x_adc.c GB18030
stm32f10x_bkp.c GB18030
stm32f10x_can.c GB18030
stm32f10x_cec.c GB18030
stm32f10x_crc.c GB18030
stm32f10x_dac.c GB18030
stm32f10x_dbgmcu.c GB18030
stm32f10x_dma.c GB18030
stm32f10x_exti.c GB18030
stm32f10x_flash.c GB18030
stm32f10x_fsmc.c GB18030
stm32f10x_gpio.c GB18030
stm32f10x_i2c.c GB18030
stm32f10x_iwdg.c GB18030
stm32f10x_pwr.c GB18030
stm32f10x_rcc.c GB18030
stm32f10x_rtc.c GB18030
stm32f10x_sdio.c GB18030
stm32f10x_spi.c GB18030
stm32f10x_tim.c GB18030
stm32f10x_usart.c GB18030
stm32f10x_wwdg.c GB18030
delay.c GB18030
delay.h GB18030
sys.c GB18030
sys.h GB18030
gui.c GB18030
gui.h GB18030
main.c GB18030
stm32f10x.h GB18030
stm32f10x_conf.h GB18030
stm32f10x_it.c GB18030
stm32f10x_it.h GB18030
system_stm32f10x.c GB18030
system_stm32f10x.h GB18030
test.c GB18030
test.h GB18030
---------错误统计------------
共0个错误!
-----------------------------

直接打开,舒服了
代码
import os
import codecs
gErrArray = []
def convert(fileName, filePath, out_enc="utf-8"):
try:
content = codecs.open(filePath, 'rb').read()
# 直接设置GB18030编码节省时间
source_encoding = 'GB18030'
print("{0:50}{1}".format(fileName, source_encoding))
if source_encoding != None:
if source_encoding == out_enc:
return
content = content.decode(source_encoding).encode(out_enc)
codecs.open(filePath, 'wb').write(content)
else:
gErrArray.append("can not recgonize file encoding %s" % filePath)
except Exception as err:
gErrArray.append("%s:%s" % (filePath, err))
def show_files(base_path):
"""
遍历当前目录所有py文件及文件夹
:param path:
:param all_files:
:return:
"""
file_list = os.listdir(base_path)
# 准备循环判断每个元素是否是文件夹还是文件,是文件的话,把名称传入list,是文件夹的话,递归
for file in file_list:
# 利用os.path.join()方法取得路径全名,并存入cur_path变量,否则每次只能遍历一层目录
cur_path = os.path.join(base_path, file)
# 判断是否是文件夹
if os.path.isdir(cur_path):
show_files(cur_path)
else:
suffix = os.path.splitext(file)[1]
if suffix == '.h' or suffix == '.c' or suffix == '.cpp' or suffix == '.hpp' or suffix == '.bat' or suffix == '.java' or suffix == '.txt':
convert(file, cur_path)
def main():
#explore(os.getcwd())
filePath = input("请输入要转换编码的文件夹路径: \n")
print("\r\n===============================================================")
print("{0:50}{1}".format('fileName', 'fileEncoding'))
print("===============================================================")
show_files(filePath)
print('\r\n---------错误统计------------')
for index, item in enumerate(gErrArray):
print(item)
print('\r\n共%d个错误!' % (len(gErrArray)))
if (len(gErrArray) > 0):
print("请检查错误文件手动修改编码")
print('\r\n-----------------------------')
if __name__ == "__main__":
main()
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!