17 September 2008

escaping xml/html with python

escaping xml/html with python is very simple. It is useful to post code excepts on the blog...

>>> from xml.sax.saxutils import escape
>>> s = """<Directory /var/www/py>
... SetHandler mod_python
... PythonHandler mod_python.publisher
... PythonDebug On
... <Files ~ "\.(gif|html|jpg|png)$">
... SetHandler default-handler
... </Files>
... </Directory>
...
... """
>>> print escape(s)
<Directory /var/www/py>
SetHandler mod_python
PythonHandler mod_python.publisher
PythonDebug On
<Files ~ "\.(gif|html|jpg|png)$">
SetHandler default-handler
</Files>
</Directory>