add_spaces.py
· 1.0 KiB · Python
Ham
import os
import re
import sys
# Add spaces between Chinese and English
def add_spaces_between_chinese_and_english(directory):
md_files = [f for f in os.listdir(directory) if f.endswith((".md", ".mdx"))]
pattern = re.compile(r'([\u4e00-\u9fff])([a-zA-Z0-9])|([a-zA-Z0-9])([\u4e00-\u9fff])')
for md_file in md_files:
md_path = os.path.join(directory, md_file)
with open(md_path, "r", encoding="utf-8") as file:
content = file.read()
new_content = pattern.sub(lambda m: f'{m.group(1) or m.group(3)} {m.group(2) or m.group(4)}', content)
with open(md_path, "w", encoding="utf-8") as file:
file.write(new_content)
print(f"Processed: {md_file}")
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python script.py <directory>")
sys.exit(1)
directory = sys.argv[1]
if not os.path.isdir(directory):
print("Error: Directory does not exist.")
sys.exit(1)
add_spaces_between_chinese_and_english(directory)
| 1 | import os |
| 2 | import re |
| 3 | import sys |
| 4 | |
| 5 | # Add spaces between Chinese and English |
| 6 | def add_spaces_between_chinese_and_english(directory): |
| 7 | md_files = [f for f in os.listdir(directory) if f.endswith((".md", ".mdx"))] |
| 8 | pattern = re.compile(r'([\u4e00-\u9fff])([a-zA-Z0-9])|([a-zA-Z0-9])([\u4e00-\u9fff])') |
| 9 | |
| 10 | for md_file in md_files: |
| 11 | md_path = os.path.join(directory, md_file) |
| 12 | with open(md_path, "r", encoding="utf-8") as file: |
| 13 | content = file.read() |
| 14 | |
| 15 | new_content = pattern.sub(lambda m: f'{m.group(1) or m.group(3)} {m.group(2) or m.group(4)}', content) |
| 16 | |
| 17 | with open(md_path, "w", encoding="utf-8") as file: |
| 18 | file.write(new_content) |
| 19 | |
| 20 | print(f"Processed: {md_file}") |
| 21 | |
| 22 | if __name__ == "__main__": |
| 23 | if len(sys.argv) != 2: |
| 24 | print("Usage: python script.py <directory>") |
| 25 | sys.exit(1) |
| 26 | |
| 27 | directory = sys.argv[1] |
| 28 | if not os.path.isdir(directory): |
| 29 | print("Error: Directory does not exist.") |
| 30 | sys.exit(1) |
| 31 | |
| 32 | add_spaces_between_chinese_and_english(directory) |