Regular expression worries

johnzenger at johnzenger at
Wed Oct 11 18:01:35 CEST 2006

You are opening the same file twice, reading its contents line-by-line
into memory, replacing "Document" with "Doc" *in memory*, never writing
that to disk, and then discarding the line you just read into memory.

If your file is short, you could read the entire thing into memory as
one string using the .read() method of fh (your file object).  Then,
call .replace on the string, and then write to disk.

If your file is long, then you want to do the replace line by line,
writing as you go to a second file.  You can later rename that file to
the original file's name and delete the original.

Also, you aren't using regular expressions at all.  You do not
therefore need the re module.

> folks
> I am new to python, so excuse me if i am asking stupid questions.
> I have a txt file  and here are some lines of it
> Document<Keyword<date:2006-08-19> Keyword<time:11:00:43>
> Keyword<username:YOURBOTNICK> Keyword<data:localhost.localdomain>
> Keyword<logon:localhost.localdomain
>   > Keyword<date:2006-08-19> Keyword<time:11:00:44> Keyword<sender:>
> Keyword<receiver:> Keyword<data::+iwx> Keyword<mode::+iwx
> I am writing a python program to replace the tags and word  Document
> with Doc.
> Here is my python program
> #! /usr/local/bin/python
> import sys
> import string
> import re
> def replace():
>   filename='/root/Desktop/project/chatlog_20060819_110043.xml.txt'
>   try:
>     fh=open(filename,'r')
>   except:
>     print 'file not opened'
>     sys.exit(1)
>   for  l in
> open('/root/Desktop/project/chatlog_20060819_110043.xml.txt'):
>       l=l.replace("Document", "DOC")
>       fh.close()
> if __name__=="__main__":
>   replace()
> But it does not replace Document with Doc in  the txt file
> Is there anything wrong i am doing
> thanks

More information about the Python-list mailing list