Skip to content

Instantly share code, notes, and snippets.

@libra146
Last active July 17, 2020 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save libra146/148bd9c38a9858cfa18287069c952c68 to your computer and use it in GitHub Desktop.
Save libra146/148bd9c38a9858cfa18287069c952c68 to your computer and use it in GitHub Desktop.
在汉字和字母数字之间添加空格,用来格式化md文档
import re
import string
import sys
import time
"""
在汉字和字母数字之间添加空格
"""
def backup(file):
with open(file + '.back', 'wb') as back_up:
with open(file, 'rb') as f:
back_up.write(f.read())
def beautify(file):
with open(file, 'r', encoding='utf8') as f:
content = f.read()
content_list = list(content)
index_list = []
string_digits = string.ascii_lowercase + string.ascii_uppercase + string.digits
for index, data in enumerate(content_list):
if index + 1 >= len(content_list):
break
next_word = content_list[index + 1]
if (re.findall(r'[\u4e00-\u9fa5]', data) and next_word in string_digits) or (
re.findall(r'[\u4e00-\u9fa5]', next_word) and data in string_digits):
index_list.append(index)
i = 0
# 在两个字中间加空格的话需要将索引加一
for a in index_list:
content_list.insert(a + i + 1, ' ')
i += 1
with open(file, 'w') as f:
f.write(''.join(content_list))
if __name__ == '__main__':
path = sys.argv[1:]
if len(path) > 1 or len(path) == 0:
print('文件过多或过少...')
time.sleep(3)
sys.exit(0)
backup(path[0])
beautify(path[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment