I'm trying to modify a find/replace script that I previously got help with here on Daniweb. The script below iterates through a file A and makes replacements defined in a csv file B. My original goal was to change any line in file A containing a search string (in whole or as a substring) defined in file B. File B contains both the search string and the string it should be changed into.
Example file A
whippy
slippy
ippy
slippy
snoob
flop
bloppy
inny
outy
slippie
blurg
lop
Example File B (substring,new form)
ippy,OOOOO
lop,TTTTTTT
But now I'd like to only make the change if there is an exact match. So the search string "ippy" should no longer cause "slippy" to change.
I tried changing "if search >= 1" to "if search == 1", but that makes zero changes.
#original find_replace.py
import string, sys, os
import csv
myOut = open('out_replace.txt', 'w')
myFile = open("test_verb_list.txt","r")
data = myFile.read()
myFile.close()
changes = csv.reader(open('test_old_new.csv', 'rb'))
for line in data:
for old, new in changes:
search = string.find(data,old)
if search >= 1:
data = data.replace(old, new)
print>>myOut, data
myOut.close()