shuichan_old/V1.5/to_gbk.py

35 lines
1.1 KiB
Python

import os, sys, io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
ROOTS = [
'V1.5/STM32F103_App1/HardWare',
'V1.5/STM32F103_App1/Src',
'V1.5/STM32F103_App1/Inc',
'V1.5/STM32F103_App1/m_app',
'V1.5/STM32F103_BootLoader/HardWare',
'V1.5/STM32F103_BootLoader/Src',
'V1.5/STM32F103_BootLoader/m_app',
]
for root in ROOTS:
for dirpath, _, files in os.walk(root):
for fn in files:
if not fn.endswith(('.c', '.h')):
continue
p = os.path.join(dirpath, fn)
raw = open(p, 'rb').read()
try:
text = raw.decode('utf-8')
except UnicodeDecodeError:
print('SKIP (not utf-8):', p)
continue
try:
out = text.encode('gbk')
warn = ''
except UnicodeEncodeError as e:
out = text.encode('gbk', errors='replace')
warn = ' [WARN 有字符被替换: %r]' % text[e.start:e.end]
if out != raw:
open(p, 'wb').write(out)
print('converted:', p, warn)