Regular expression worries

Tim Chase python.list at tim.thechases.com
Wed Oct 11 12:11:09 EDT 2006


>   for  l in
> open('/root/Desktop/project/chatlog_20060819_110043.xml.txt'):
> 
>       l=l.replace("Document", "DOC")
>       fh.close()
> 
> But it does not replace Document with Doc in  the txt file

In addition to closing the file handle for the loop *within* the 
loop, you're changing "l" (side note: a bad choice of names, as 
in most fonts, it's difficult to visually discern from the number 
"1"), but you're not writing it back out any place.  One would do 
something like

outfile = open('out.txt', 'w')
infile = open(filename)
for line in infile:
	outfile.write(line.replace("Document", "DOC"))
outfile.close()
infile.close()

You could even let garbage collection take care of the file 
handle for you:


outfile = open('out.txt', 'w')
for line in open(filename):
	outfile.write(line.replace("Document", "DOC"))
outfile.close()


If needed, you can then move the 'out.txt' overtop of the 
original file.

Or, you could just use

	sed 's/Document/DOC/g' $FILENAME > out.txt

or with an accepting version, do it in-place with

	sed -i 's/Document/DOC/g' $FILENAME

if you have sed available on your system.

Oh...and it doesn't look like your code is using regexps for 
anything, despite the subject-line of your email :)  I suspect 
they'll come in later for the "replace the tags" portion you 
mentioned, but that ain't in the code.

-tkc








More information about the Python-list mailing list