Python Forum
How do you replace a word after a match from a list of words in each line of a file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do you replace a word after a match from a list of words in each line of a file?
#1
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()
Output:
Expected Output: This is Manager * speaking.Hello, how are you? I am Fine. this is Director * Nice to Meet you Manager *** But, Output I am getting now: * T* h*** i*** s*** * i* s*** * M* a*** n*** a*** g*** e*** r*** * S* a*** m*** * s* p*** e*** a*** k*** i*** n*** g*** .* H* e*** l*** l*** o*** ,* * h*** o*** w*** * a* r*** e*** * y* o*** u*** ?***
Reply
#2
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()
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Replace a text/word in docx file using Python Devan 4 2,854 Oct-17-2023, 06:03 PM
Last Post: Devan
  Function to count words in a list up to and including Sam Oldman45 15 6,409 Sep-08-2023, 01:10 PM
Last Post: Pedroski55
  Need to replace a string with a file (HTML file) tester_V 1 699 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,392 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Working with Excel and Word, Several Questions Regarding Find and Replace Brandon_Pickert 4 1,492 Feb-11-2023, 03:59 PM
Last Post: Brandon_Pickert
  Getting last line of each line occurrence in a file tester_V 1 812 Jan-31-2023, 09:29 PM
Last Post: deanhystad
  Replace columns indexes reading a XSLX file Larry1888 2 951 Nov-18-2022, 10:16 PM
Last Post: Pedroski55
  Need to match two words in a line tester_V 2 845 Nov-18-2022, 03:13 AM
Last Post: tester_V
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,306 Sep-27-2022, 01:38 PM
Last Post: buran
  For Word, Count in List (Counts.Items()) new_coder_231013 6 2,497 Jul-21-2022, 02:51 PM
Last Post: new_coder_231013

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020