editing a HTML file

Dave Angel davea at davea.name
Fri Mar 15 02:31:19 CET 2013

On 03/14/2013 02:09 PM, Tracubik wrote:
> Hi all,
> I'would like to make a script that automatically change some text in a
> html file.
> I need to make some changes in the text of <p> tags
> My question is: there is a way to just "update/substitute" the text in
> the html <p> tags or do i have to make a new modified copy of the html
> file?
> To be clear, i'ld like to make something like this:
> open html file
> for every <p> tags:
>    if "foo" in text:
>      change "foo" in "bar"
> close html file
> any sample would be really appreciated
> I'm really a beginner as you can see
> Thanks

As JM points out, you can use Beautiful Soup to parse html.  Then you 
can make structural changes, and write it back out.  Beautiful Soup is 
NOT part of the standard library.

But if you haven't already written something that modifies regular text 
files, I'd do that long before I even started messing with html.  You 
cannot in general update things in place, so you have to think about the 
mechanics of updating, and of minimizing or eliminating the likelihood 
of losing data.

For example, suppose you have a text file (created with any text editor) 
that has just one occurrence of the string "Sammy".  You want to replace 
that with the word "Gazelda".  Notice the replacement string is longer 
than the original.  Think about how you'd go about it, and write the 
simplest program that would accomplish it.  Then think about what could 
go wrong.  What about if somebody shuts the machine off just as you're 
starting to rewrite the file, or the program crashes just then, or 
whatever ?  So plan to write the replacement file to a new name, and 
after written, do the appropriate renames and delete of the old one.

Don't forget about closing each file, especially if you're going to 
manipulate it with other functions.


More information about the Python-list mailing list