Last active
August 26, 2024 15:21
-
-
Save xiabingquan/613f26b112d68830061542c83efa16d1 to your computer and use it in GitHub Desktop.
A super easy (but useful) script to insert whitespaces to English words which are adjacent to Chinese.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import sys | |
def insert_spaces(text): | |
text = re.sub(r'([a-zA-Z])([\u4e00-\u9fa5])', r'\1 \2', text) | |
text = re.sub(r'([\u4e00-\u9fa5])([a-zA-Z])', r'\1 \2', text) | |
return text | |
if __name__ == '__main__': | |
if len(sys.argv)!= 2: | |
print('Usage: python replace.py <markdown_file>') | |
md_file = sys.argv[1] | |
# read file | |
with open(md_file, 'r', encoding='utf-8') as file: | |
content = file.read() | |
# insert spaces between Chinese and English words | |
content = insert_spaces(content) | |
# write modified content back to the file | |
with open(md_file, 'w', encoding='utf-8') as file: | |
file.write(content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment