I/O

Steve Holden sholden at holdenweb.com
Tue Dec 17 23:33:24 EST 2002


"Peter Scott" <sketerpot at chase3000.com> wrote in message
news:atobng$15p11$1 at ID-174764.news.dfncis.de...
> Tee Bee wrote:
> > My first problem at the moment is, that I got a 1 or 2 dozen of
HTML-files
> > with text-passages that need to be changed or deleted.
> >
> > f.e.:
> >
> > 1. <b>this is a text-passage</b>    <= Content of the file.
> >
> > 2. read every byte
> >
> > 3. compare
> >
> > 4. delete or change content
>
> That looks like a job for sed or perl. With those you could do
> "s/<b>this is a text-passage</b>/<b>This is what to replace it with/g"
> and just run this sed script on all the files you want. The perl syntax
> is similar.
>
Bzzzt. The string you are searching for conatins a slash, so you can't use a
slash as the RE delimiter in sed. I seem to remember sed lets you use almost
any delimiter, though, so I guess the OP might try

    s%<b>this is a text-passage</b>%<b>This is what to replace it with%g

although I don't see why you are deleting the closing </b> tag arbitrarily.
Typo?

> If you really want to use Python, such as if you have this as part of a
> larger application or want Python's portability, I believe that you
> could go through all your files and use "string.replace(myString,
> '<b>this is a text-passage</b>', '<b>This is what to replace it with')"
>
> Hopefully those should do the trick.
>
Yes, but a) you don't show how to process each line through the myString
variable, and b) nowadays unless you are using Python 1.x it's better to use
the string method. As in the following untested code...

    import sys
    for filename in sys.argv[1:]:
        myString = file(filename, "rb").read()
        file(filename, "wb").write(myString.replace("something",
"targetstring"))

The program, if it works, will dangerously process all files named in thje
command line, replacing all occurrence of "something" with "targetstring".
It is left as an exercise for the reader to explain why, if anything goes
wrong, with the file processing information might likely be lost or
destroyed :-)

regards
-----------------------------------------------------------------------
Steve Holden                                  http://www.holdenweb.com/
Python Web Programming                 http://pydish.holdenweb.com/pwp/
Bring your musical instrument to PyCon!    http://www.python.org/pycon/
-----------------------------------------------------------------------






More information about the Python-list mailing list