better way than: myPage += 'more html' , ...

Thomas Güttler guettler at thomas-guettler.de
Thu Jun 26 02:56:51 EDT 2003


Gobo Borz wrote:

> Hi everyone,
> 
> I have a python cgi program that uses print statements to write html.
> The program has grown, and for reasons I won't bore you with, I need to
> build the page in a string and "print" it at once.

Hi!

I do it like this:

def foo(mydict):
    ret=[]

    rows=[]
    for key, value in mydict.items():
        rows.append('<tr><td>%s</td><td>%s</td></tr>' % (key, value))
    rows=''.join(rows)

    ret.append('<table>%s</table>' % rows)
    return ''.join(ret)

I try to keep the start-tag and the end-tag in one string.
This keeps the code from becomming ugly.

 thomas





More information about the Python-list mailing list