HTML parser example, anybody?

Bjorn Pettersen bjorn at roguewave.com
Thu Apr 27 15:10:21 EDT 2000


Gerhard Haering wrote:
> 
> Hi!
> 
> I want to transform my own HTML templates (with a few template tags,
> special comments, and so on) into final HTML for the browser. I would like
> to use Pyhton's htmllib, but from the docs I can get no clue on how to get
> it done. I seem to have to use a writer and a formatter class ...
> 
> I would greatly appreciate an example of HTML -> HTML using
> htmllib/sgmllib. Can anybody provide pointers/code snippets?

Why don't you just use Python's string syntax, e.g. if you have the
following in a template file called foo.templ:

   <html><head><title>%(title)s</title></head>
   <body><h1>%(heading)s</h1></body></html>

you can do something like:

   htmlfile = open('foo.html', 'w')
   replacements = {
     'title':   'my title',
     'heading': 'A Heading',
   }
   htmlfile.write( open(foo.templ).read() % replacements )
   htmlfile.close()

-- bjorn




More information about the Python-list mailing list