7
print u'<'

How can I print <

print '>' 

How can I print &gt;

Sathyajith Bhat
  • 20,587
  • 21
  • 94
  • 129
zjm1126
  • 58,281
  • 75
  • 169
  • 215
  • 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
  • 27,813
  • 15
  • 71
  • 89