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
  Trying to get the first letter of every word in a list DanielCook 2 153 Jan-05-2021, 05:06 PM
Last Post: deanhystad
  Passing List of Objects in Command Line Python usman 7 561 Sep-27-2020, 03:45 PM
Last Post: ndc85430
  Counting the most relevant words in a text file caiomartins 2 301 Sep-21-2020, 08:39 AM
Last Post: caiomartins
  Recognising mathematical expressions from word and pdf file Preeti15 0 297 Aug-19-2020, 09:06 AM
Last Post: Preeti15
  How to create and define in one line a 2D list of class objects in Python T2ioTD 1 295 Aug-14-2020, 12:37 PM
Last Post: Yoriz
  Trying to find first 2 letter word in a list of words Oldman45 7 529 Aug-11-2020, 08:59 AM
Last Post: Oldman45
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 772 Aug-10-2020, 11:01 PM
Last Post: medatib531
  Read/Sort Large text file avoiding line-by-line read using mmep or hdf5 Robotguy 0 421 Jul-22-2020, 08:11 PM
Last Post: Robotguy
  how to check if string contains ALL words from the list? zarize 6 586 Jul-22-2020, 07:04 PM
Last Post: zarize
  How to extract a single word from a text file buttercup 7 585 Jul-22-2020, 04:45 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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