[Tutor] re.compile and objects - what am doing wrong?

Mark Boydell markboydell at gmail.com
Mon Dec 27 19:18:50 CET 2004


Been a long time since I last posted to the list so this is only my
third program in as many years :(

Anyway the basic idea is to look through two csv files (seperated by
tabs) enter both of them into dictionaries and compare the keys of the
"polished" version with the "rough" one. If I've already dealt with
that entry in some form or way in the polished version, I want to bin
that entry in the rough dictionary. So, for example, if I already have
an entry called "Woody Allen" in the polished version, I'd want to bin
any entry in rough version I have that contains the text Woody Allen
in it (like *Woody Allen* ).
I got pretty close I think but I'm stumbling with re.compile which
seems to only take hard wired text. Can't I assign the object key to
it?

Here's my code so far (and yes my aesthetics of my code has not
improved much over the last few years - I guess I'm a slow learner).
Any ideas what I should do here?

#! /usr/bin/python
import string, re 

dict1={}
dict2={}
inputFile1 = open("rough.txt", "rb")
inputFile2 = open("polished.txt", "rb")

for row in inputFile1.readlines():
	words = string.split(row,"\t") 
	dict1[words[0]]=words[1]
	
for row in inputFile2.readlines():
	words = string.split(row,"\t") 
	dict2[words[0]]=words[1]

outFile1 = open ("rough.out", "w")
outFile2 = open ("polish.out", "w")
polishKeys=dict2.keys()
roughKeys=dict1.keys()
for key in polishKeys:
	searchPat=re.compile("%s") % key # Doesn't work 
	for rKey in roughKeys:
		if searchPat.match(rKey): print key+"----"+rKey
 
outFile1.close()
outFile2.close()
inputFile1.close()
inputFile2.close()


More information about the Tutor mailing list