專案遷移或跨平台協作時,常需將大量原始碼/文件從 GBK (或 Big5) 批量轉為 UTF-8。本文提供三個版本的轉換腳本,分別適用於 Linux (iconv)、Python 2.6+ 與 Python 3.1+,支援遞迴目錄、編碼偵測與錯誤處理。
#!/usr/bin/python
# coding=utf-8
import os, commands
def trans(dirname):
for entry in os.listdir(dirname):
path = os.path.join(dirname, entry)
if os.path.isdir(path):
trans(path)
else:
# 僅處理文字檔且非 .htm/.xml
if entry.endswith(('.htm', '.xml')):
continue
mime = commands.getoutput(f'file -i {path}')
if 'text/' in mime and 'iso-8859' in mime: # file 偵測為 ISO-8859 通常實為 GBK
print(f'Converting {path}')
commands.getoutput(f'iconv -f gbk -t utf-8 -c -o "{path}" "{path}"')
trans(os.getcwd())
注意:file -i 偵測編碼非 100% 準確,建議先備份或加入白名單機制。
import os, sys
def convert(filename, in_enc='GBK', out_enc='UTF-8'):
try:
print(f"convert {filename}")
with open(filename, 'rb') as f:
content = f.read()
new_content = content.decode(in_enc).encode(out_enc)
with open(filename, 'wb') as f:
f.write(new_content)
print(" done")
except Exception as e:
print(f" error: {e}")
def explore(root_dir):
for root, _, files in os.walk(root_dir):
for f in files:
convert(os.path.join(root, f))
if __name__ == '__main__':
for path in sys.argv[1:] or ['.']:
if os.path.isfile(path):
convert(path)
elif os.path.isdir(path):
explore(path)
import os, sys, codecs
def convert(file, in_enc='GBK', out_enc='UTF-8'):
try:
print(f"convert {file}")
with codecs.open(file, 'r', in_enc) as f:
content = f.read()
with codecs.open(file, 'w', out_enc) as f:
f.write(content)
except IOError as e:
print(f"I/O error: {e}")
def explore(root_dir):
for root, _, files in os.walk(root_dir):
for f in files:
convert(os.path.join(root, f))
if __name__ == '__main__':
for path in sys.argv[1:] or ['.']:
if os.path.isfile(path):
convert(path)
elif os.path.isdir(path):
explore(path)
cp -r 整個目錄。.py .js .css .html .txt .java .c .h .cpp .md 等文字檔,避免損壞圖片/二進位檔。chardet 或 cchardet 自動偵測來源編碼,而非假設皆為 GBK。# 將當前目錄下所有 .py 檔從 GBK 轉 UTF-8
find . -name '*.py' -exec iconv -f gbk -t utf-8 -c -o {}.utf8 {} \; -exec mv {}.utf8 {} \;
選擇適合環境的版本,配合備份與白名單機制,即可安全完成大規模編碼遷移。