[Tutor] search-replace

Tommy Kaas tommy.kaas at kaasogmulvad.dk
Mon Jun 6 10:05:26 CEST 2011


Hi tutors

 

If I need to clean a textfile (perhaps after web scraping), I have used this
method - without problems - but I'm sure there must be smarter and better
ways. 

I'm especially interested to know how I do more than just one search-replace
without having to repeat the whole step below.

 

I'm using Python 2.6.6 on a windows pc.

 

fin = open("dirtyfile.txt")

fout = open("cleanfile.txt", "w")

for line in fin:

    fout.write(line.replace('## ', '#')) 

fin.close()

fout.close()

 

Thanks,

Tommy

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110606/f493052d/attachment-0001.html>


More information about the Tutor mailing list