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()

Recommended Answers

All 2 Replies

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.

Don't you just want to test for equality then? Say (e.g.): if line[:-1] == 'ippy': instead of using string.find()? That would only match "ippy".

Shouldn't you be using line instead of data everywhere after the for line in data: statement?

The basic problem that I'm encountering is the differing contents of a "line". When I add a print data statement after "data = myFile.read()", (print 'data = ', data) it shows each line containing one word, exactly like the input file.
data = whippy
slippy
ippy
...

But when I put a print line statement after "for line in data:", it prints each letter as a single line, like this (print ' for line =', line)
for line = w
for line = h
for line = i
for line = p
for line = p
for line = y
...

So when I try to use an identity statement like "if line[:-1] == 'ippy'", it fails because it's trying to match up 4 characters to one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.