Files
akmon/fix_encoding.py
2026-01-20 08:04:15 +08:00

107 lines
2.6 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import os
def fix_corrupted_chars(content):
# 损坏字符映射表
char_map = {
'杩斿洖': '返回',
'用户名璇︽儏': '用户名详情',
'娣诲姞瑙掕壊': '添加角色',
'鍩烘湰淇℃伅': '基本信息',
'瑙掕壊涓庢潈': '角色与权限',
'璇ョ敤鎴锋湭鍒嗛厤浠讳綍瑙掕壊': '该用户未分配任何角色',
'宸查攢姣': '已启用',
'宸查攢鐢': '已禁用',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': ''
}
for corrupted, correct in char_map.items():
content = content.replace(corrupted, correct)
return content
def main():
if len(sys.argv) != 2:
print("Usage: python fix_encoding.py <file_path>")
sys.exit(1)
file_path = sys.argv[1]
if not os.path.exists(file_path):
print(f"File not found: {file_path}")
sys.exit(1)
try:
# Read file with UTF-8 encoding
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Fix corrupted characters
fixed_content = fix_corrupted_chars(content)
# Write back the fixed content
with open(file_path, 'w', encoding='utf-8') as f:
f.write(fixed_content)
print(f"Fixed encoding issues in {file_path}")
except Exception as e:
print(f"Error processing file: {e}")
sys.exit(1)
if __name__ == "__main__":
main()