Skip to content

Instantly share code, notes, and snippets.

@xiabingquan
Last active August 26, 2024 15:21
Show Gist options
  • Save xiabingquan/613f26b112d68830061542c83efa16d1 to your computer and use it in GitHub Desktop.
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.
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