如果没有空格,我们将尝试在文件中每一行的搜索词列表中的匹配词前后添加一个空格。
输入: 嗨,我是山姆经理。 你好,我是 Director.Tom。山姆经理先生,今天过得怎么样。
import re
f1=open('input.txt', 'r')
f2=open('outout.txt', 'w')
checkWords = ("Manager",Director)
for line in f1:
for checkword in checkWords:
line = re.sub(r'(\b${0}\b)'.format(checkword), r'\1 ', line)
print(line)
f2.write(line)
f1.close()
f2.close()
预期输出: 嗨,我是山姆经理。 你好,我是 .Tom 主任。山姆经理先生,今天过得怎么样。
请您参考如下方法:
也许您可以使用(校验字索引 - 1)和(校验字索引 + 单词长度)来检查那里是否有空格。然后您可以相应地使用 replace()。
