Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41235 > unrolled thread

editing a HTML file

Started byTracubik <affdfsdfdsfsd@b.com>
First post2013-03-14 19:09 +0100
Last post2013-03-14 21:31 -0400
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  editing a HTML file Tracubik <affdfsdfdsfsd@b.com> - 2013-03-14 19:09 +0100
    Re: editing a HTML file Dave Angel <davea@davea.name> - 2013-03-14 21:31 -0400

#41235 — editing a HTML file

FromTracubik <affdfsdfdsfsd@b.com>
Date2013-03-14 19:09 +0100
Subjectediting a HTML file
Message-ID<51421253$0$26783$4fafbaef@reader2.news.tin.it>
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

[toc] | [next] | [standalone]


#41253

FromDave Angel <davea@davea.name>
Date2013-03-14 21:31 -0400
Message-ID<mailman.3327.1363311098.2939.python-list@python.org>
In reply to#41235
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.



-- 
DaveA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web