replacing words in HTML file

Daniel Fetchinson fetchinson at googlemail.com
Thu Apr 29 11:46:16 EDT 2010


>> > | > Any idea how I can replace words in a html file? Meaning only the
>> > | > content will get replace while the html tags, javascript, & css are
>> > | > remain untouch.
>> > |
>> > | I'm not sure what you tried and what you haven't but as a first trial
>> > | you might want to
>> > |
>> > | <untested>
>> > |
>> > | f = open( 'new.html', 'w' )
>> > | f.write( open( 'index.html' ).read( ).replace( 'replace-this',
>> > 'with-that'
>> > ) )
>> > | f.close( )
>> > |
>> > | </untested>
>>
>> > If 'replace-this' occurs inside the javascript etc or happens to be an
>> > HTML tag name, it will get mangled. The OP didn't want that.
>>
>> Correct, that is why I started with "I'm not sure what you tried and
>> what you haven't but as a first trial you might". For instance if the
>> OP wants to replace words which he knows are not in javascript and/or
>> css and he knows that these words are also not in html attribute
>> names/values, etc, etc, then the above approach would work, in which
>> case BeautifulSoup is a gigantic overkill. The OP needs to specify
>> more clearly what he wants, before really useful advice can be given.
>
> Funny, everyone else understood what the OP meant, and useful advice
> was given.

It was a lucky day for the OP then!

:)

Cheers,
Daniel


-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown



More information about the Python-list mailing list