0

I am trying to use a lookup table to replace values in a text file

The format of the lookup table is like

Search             Replace
Unit Of Measure    Unit of Measure
Bill Of Material   Bill of Material
Reference Item     Reference Material Number

I am using re.sub(Search, Replace, File)

I need to do something like (r'\b Unit Of Measure\b', Replace, File) for every iteration.

I am not able to put these tags into a variable.

Could you please help?

Below is the code

import re
f1 = open('C:\Scripts\FNR\SearchReplacekey.txt','r')
f = open("C:\Scripts\FNR\log.txt",'a')
f.seek(0)
f.truncate()
f.close()
for line in f1:
    fields = line.split('|')
    a = fields[0]
    Srch = fields[1]
    Repl = fields[2]
    f = open("C:\Scripts\FNR\Output.txt",'r')
    filedata = f.read()
    check = re.search(Srch,filedata)
    f.close()

    if check == -1:

        f = open("C:\Scripts\FNR\log.txt",'a')
        newdata = a + ' ' + Srch + ' ' + Repl
        f.write(newdata + "\n")
        f.close()
    else:

        # the problem is here I need to force Srch to match exact word
        newdata = re.sub(Srch,Repl,filedata)

        f = open("C:\Scripts\FNR\Output.txt",'w')
        f.write(newdata)
        f.close()

Thanks, Kowsik

3
  • Could you provide some more code? Commented Feb 1, 2017 at 14:57
  • See stackoverflow.com/questions/3543559/… Commented Feb 1, 2017 at 14:57
  • Code has been added Commented Feb 1, 2017 at 16:48

2 Answers 2

0

I'm not sure what you're asking but could it be that:

searchs = ["Unit Of Measure", "Bill Of Material"]
for s in searchs:
  re.sub(s, Replace, File)
Sign up to request clarification or add additional context in comments.

1 Comment

I am not asking that For example. Consider the below text line1 .'a Unit Of Measure line2 . BaseUnit Of Measure' I need to replace only line1 and not line 2 I know for this we need to use re.sub(r'\b Unit Of Measure\b', Replace, File) But I am getting the search string from a variable which is coming from a different file. My question is what changes do i need to make so the variable can can be used to search for the exact word
0
>>>searchPattern = "Unit Of Measure"
>>>replacePatter = "Unit of Measure"
>>>sampleString = "not Unit Of measure, but Unit Of Measure and not Unit Of"
>>>re.sub(searchPattern,replacePatter, sampleString)
'not Unit Of measure, but Unit of Measure and not Unit Of'

Maybe I am missing something here, but if you enclose the patterns in quotes this seems to work well enough.

1 Comment

this is coming from a file so i wont be able to edit anything, plus i am trying to use exact match , which will work only with r('\bSearchString\b'),I am not able to add this to the search string as it comes from a file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.