multiple replaces

D H no at spam
Fri Sep 16 10:33:31 EDT 2005


You can use python's re.sub function.  But also look into full fledged 
template engines like Cheetah.

import re

txt="""
<html>
<body>
<p>whatever</p>
<!--tree-->
<p>the machine with bing</p>
<!--house-->
<p>10% of boo is foo</p>
</html>"""

h1=txt.replace("%","%%")
h3 = re.sub("<!--(\w+)-->", "%(\\1)s", h1)

house="something awfull"
tree="something beautifull"
print h3 % locals()




More information about the Python-list mailing list