How do you replace a word after a match from a list of words in each line of a file? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How do you replace a word after a match from a list of words in each line of a file? (/thread-21839.html) |
How do you replace a word after a match from a list of words in each line of a file? - vijju56 - Oct-17-2019 We are looking for replacing a word after each match in a list of words for each line in a file Quote:This is Manager Sam speaking.Hello, how are you? I am Fine. this is Director Tom. Nice to Meet you Manager Sam. import re f1=open('testinput.txt', 'r') f2=open('testoutput.txt', 'w') checkWords = ["Manager","Director"] repWords = ("*** ") for line in f1: i = 0 for i in range(len(checkWords)): # Find the next word after the search word list1 = re.compile(r'%s\s+((?:\w+(?:\s+!$)) {1})' %checkWords[i]).findall(line) checkWords = ','.join(list1) print(checkWords) line = line.replace(checkWords, repWords) print(line) f2.write(line) f1.close() f2.close()
RE: How do you replace a word after a match from a list of words in each line of a file? - baquerik - Oct-17-2019 Hello vijjuu, 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() 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.
|