473,385 Members | 1,402 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

help for newbie -- using find() for exact match

I'm trying to modify a find/replace script which 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.

I tried a simple test for equality like this "if line == old: data = data.replace(old, new)", but this fails because the lines within the for loop are just single characters, not whole words. For example, if 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
...

Any help would be greatly appreciated.
Expand|Select|Wrap|Line Numbers
  1. #original find_replace.py
  2. import string, sys, os
  3. import csv
  4.  
  5. myOut = open('out_replace.txt', 'w')
  6. myFile = open("test_verb_list.txt","r")
  7. data = myFile.read()
  8. myFile.close()
  9.  
  10. changes = csv.reader(open('test_old_new.csv', 'rb'))
  11.  
  12. for line in data:
  13.        for old, new in changes:
  14.                search = string.find(data,old)
  15.                if search >= 1:
  16.                      data = data.replace(old, new)
  17.  
  18. print>>myOut, data
  19. myOut.close()
  20.  
May 2 '08 #1
2 7198
bvdet
2,851 Expert Mod 2GB
I'm trying to modify a find/replace script which 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.

I tried a simple test for equality like this "if line == old: data = data.replace(old, new)", but this fails because the lines within the for loop are just single characters, not whole words. For example, if 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
...

Any help would be greatly appreciated.
Expand|Select|Wrap|Line Numbers
  1. #original find_replace.py
  2. import string, sys, os
  3. import csv
  4.  
  5. myOut = open('out_replace.txt', 'w')
  6. myFile = open("test_verb_list.txt","r")
  7. data = myFile.read()
  8. myFile.close()
  9.  
  10. changes = csv.reader(open('test_old_new.csv', 'rb'))
  11.  
  12. for line in data:
  13.        for old, new in changes:
  14.                search = string.find(data,old)
  15.                if search >= 1:
  16.                      data = data.replace(old, new)
  17.  
  18. print>>myOut, data
  19. myOut.close()
  20.  
It would be appropriate to create a dictionary from the csv file data. You can test if each line, stripped of any whitespace, is in the dictionary with the in operator. Example:
Expand|Select|Wrap|Line Numbers
  1. import csv
  2.  
  3. csv_file = 'data_csv.csv'
  4. data_file = 'data.txt'
  5.  
  6. change_list = [obj for obj in csv.reader(open(csv_file, 'rb'))]
  7. changes_dict = dict(zip([item[0] for item in change_list],[item[1] for item in change_list]))
  8. f = open(data_file)
  9. for line in f:
  10.     line = line.strip()
  11.     print 'Original text: %s' % line
  12.     line = changes_dict.get(line, line)
  13.     print 'Final text: %s' % line
  14.  
  15. f.close()
May 2 '08 #2
EXCELLENT! This worked perfectly. Thread SOLVED!! Thanks!
May 3 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Steve | last post by:
Hello, I am writing a script that calls a URL and reads the resulting HTML into a function that strips out everthing and returns ONLY the links, this is so that I can build a link index of various...
7
by: Reckless | last post by:
I've got a file with this in it: The data I'd like extracted is within the quotes: Some string data I can read the file out and extract (using string positions) the data I'd like but it...
7
by: OBJECT MODULE, INC. | last post by:
table 1 APP_ID 1 3 table 2
8
by: Jack Addington | last post by:
I want to scroll through the alphabet in order to scroll some data to the closest name that starts with a letter. If the user hits the H button then it should scroll to the letter closest to H. ...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
2
by: Tony Ciconte | last post by:
Does anyone know of or have any VBA code or similar logic that can help distinguish similar first/last name combinations? For example, we would like to prompt the user of a possible match when any...
4
by: jmdaviault | last post by:
I want to do the equivalent of SELECT id from TABLE WHERE text='text' only fast solution I found is: SELECT id,text from TABLE WHERE MATCH(text) AGAINST('value' IN BOOLEAN MODE) HAVING...
36
by: Jules | last post by:
All, how do I describe a string consisting of any number of characters, with an optional (but unique when occuring) end-of-line expression '/'? With groups if possible, as in...
6
Gaiason
by: Gaiason | last post by:
Hi XML/XSLT masters and gurus, I am a newbie in XML/XSLT and have been reading up XML/XSLT in order to convert a XML generated from a OCR engine to another format of XML. I am truly stuck at this...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.