7
print u'<'

How can I print <

print '>' 

How can I print &gt;

Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
zjm1126
  • 63,397
  • 81
  • 173
  • 221
  • I don't do Python, but those are called "HTML entities". If you poke around using that new keyword in Google, you may find enough information: http://www.google.com/search?q=python+html+entities – BalusC Dec 30 '09 at 01:09
  • 1
    That's not likely to be enough, BalusC. The OP makes it clear in his profile that explanations in English are difficult to understand, he wants **code**. *Consider that the QUESTION is in the form of code* – pavium Dec 30 '09 at 01:14
  • 3
    yeah, BalusC, send him teh c0dez – SilentGhost Dec 30 '09 at 01:16

1 Answers1

17

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;
Community
  • 1
  • 1
jbochi
  • 28,816
  • 16
  • 73
  • 90