|
@@ -0,0 +1,73 @@
|
|
|
|
|
+import os
|
|
|
|
|
+
|
|
|
|
|
+# -------------------------- 核心配置 --------------------------
|
|
|
|
|
+# 目标文件夹路径(修改为你的根文件夹路径)
|
|
|
|
|
+TARGET_FOLDER = r"F:\找乱码20260309\乱码文件\8"
|
|
|
|
|
+# -------------------------------------------------------------
|
|
|
|
|
+
|
|
|
|
|
+# 全量变形字符映射表(补全a-z所有字符)
|
|
|
|
|
+char_mapping = {
|
|
|
|
|
+ # 小写 a-z 完整映射
|
|
|
|
|
+ '犪': 'a', '犆': 'b', '犮': 'c', '犱': 'd', '犲': 'e', '犳': 'f',
|
|
|
|
|
+ '犵': 'g', '犺': 'h', '犻': 'i', '犼': 'j', '犽': 'k', '犾': 'l',
|
|
|
|
|
+ '犿': 'm', '狀': 'n', '狅': 'o', '狆': 'p', '狇': 'q', '狉': 'r',
|
|
|
|
|
+ '狊': 's', '狋': 't', '狌': 'u', '狏': 'v', '狑': 'w', '狓': 'x',
|
|
|
|
|
+ '狔': 'y', '狕': 'z',
|
|
|
|
|
+ # 大写 A-Z 映射
|
|
|
|
|
+ '犃': 'A', '犅': 'B', '犆': 'C', '犇': 'D', '犈': 'E', '犉': 'F',
|
|
|
|
|
+ '犌': 'G', '犎': 'H', '犐': 'I', '犑': 'J', '犓': 'K', '犔': 'L',
|
|
|
|
|
+ '犕': 'M', '犖': 'N', '犗': 'O', '犘': 'P', '犙': 'Q', '犚': 'R',
|
|
|
|
|
+ '犛': 'S', '犜': 'T', '犝': 'U', '犞': 'V', '犠': 'W', '犡': 'X',
|
|
|
|
|
+ '犢': 'Y', '犣': 'Z'
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+def restore_special_chars(content):
|
|
|
|
|
+ """将文本中的变形字符替换为常规英文字母"""
|
|
|
|
|
+ restored_content = content
|
|
|
|
|
+ for special_char, normal_char in char_mapping.items():
|
|
|
|
|
+ restored_content = restored_content.replace(special_char, normal_char)
|
|
|
|
|
+ return restored_content
|
|
|
|
|
+
|
|
|
|
|
+def process_single_md_file(file_path):
|
|
|
|
|
+ """处理单个md文件"""
|
|
|
|
|
+ filename = os.path.basename(file_path)
|
|
|
|
|
+ print(f"正在处理文件:{file_path}")
|
|
|
|
|
+
|
|
|
|
|
+ # 读取文件
|
|
|
|
|
+ try:
|
|
|
|
|
+ with open(file_path, 'r', encoding='utf-8') as f:
|
|
|
|
|
+ original_content = f.read()
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ print(f"读取文件 {filename} 失败:{e}")
|
|
|
|
|
+ return
|
|
|
|
|
+
|
|
|
|
|
+ # 替换字符
|
|
|
|
|
+ restored_content = restore_special_chars(original_content)
|
|
|
|
|
+
|
|
|
|
|
+ # 写入文件
|
|
|
|
|
+ try:
|
|
|
|
|
+ with open(file_path, 'w', encoding='utf-8') as f:
|
|
|
|
|
+ f.write(restored_content)
|
|
|
|
|
+ print(f"文件 {filename} 处理完成!\n")
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ print(f"写入文件 {filename} 失败:{e}\n")
|
|
|
|
|
+
|
|
|
|
|
+def process_md_files_recursive(folder_path):
|
|
|
|
|
+ """递归处理所有子文件夹中的md文件"""
|
|
|
|
|
+ if not os.path.exists(folder_path):
|
|
|
|
|
+ print(f"错误:文件夹 {folder_path} 不存在!")
|
|
|
|
|
+ return
|
|
|
|
|
+
|
|
|
|
|
+ for item in os.listdir(folder_path):
|
|
|
|
|
+ item_path = os.path.join(folder_path, item)
|
|
|
|
|
+ # 递归处理子文件夹
|
|
|
|
|
+ if os.path.isdir(item_path):
|
|
|
|
|
+ process_md_files_recursive(item_path)
|
|
|
|
|
+ # 处理md文件
|
|
|
|
|
+ elif item.lower().endswith('.md'):
|
|
|
|
|
+ process_single_md_file(item_path)
|
|
|
|
|
+
|
|
|
|
|
+if __name__ == "__main__":
|
|
|
|
|
+ print("开始递归处理所有MD文件(含子文件夹)...")
|
|
|
|
|
+ process_md_files_recursive(TARGET_FOLDER)
|
|
|
|
|
+ print("所有MD文件处理完成!")
|