tags:

views:

162

answers:

2
print u'&lt;'#how can i print '<'
print '>' #how can i print '&gt;'

thanks

A: 

[Obligatory "use a DOM to construct XML, in the name of all that is holy" post.]

Robert Rossney
+1  A: 

You should use HTMLParser module to decode html:

>>> import HTMLParser
>>> h= HTMLParser.HTMLParser()
>>> h.unescape('alpha &lt; &beta;')
u'alpha < \u03b2'

To escape HTML, cgi module is fine:

>>> cgi.escape(u'<a>bá</a>').encode('ascii', 'xmlcharrefreplace')
'&lt;a&gt;b&#225;&lt;/a&gt;
jbochi
+1 for HTMLParser and the great links.
Adam Bernier