Hello vijjuu,
there are so many stars in your output because this line of code returns nothing and every character is then a match
there are so many stars in your output because this line of code returns nothing and every character is then a match
list1 = re.compile(r'%s\s+((?:\w+(?:\s+!$)) {1})' %checkWords[i]).findall(line)There must be way better ways of doing this, but this code gives the output you were asking for:
f1=open('testinput.txt', 'r') f2=open('testoutput.txt', 'w') checkWords = ["Manager","Director"] repWords = ("*") for line in f1: words = line.split(" ") for i in range(len(words)): for checkword in checkWords: if words[i] == checkword: words[i+1] = repWords newline = "" for word in words: newline = newline + word + " " print(newline) f2.write(newline) f1.close() f2.close()
Output:This is Manager * speaking.Hello, how are you? I am Fine. this is Director * Nice to Meet you Manager *
Problems: It does not "understand" what a word is, so if punctuation symbols are involved (in the matched word or in the replacement) it fails.