We are trying to add a space before and after a matched word from a list of search words for each line in a file, if there isn't a space.
Input: Hi This is Manager Sam. Hello, this is Director.Tom. How is your Day Mr.Manager Sam.
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()
Expected Output: Hi This is Manager Sam. Hello, this is Director .Tom. How is your Day Mr. Manager Sam.