开发者

How can I change '>' to '>' and '>' to '>'? [duplicate]

This question already has answers here: Decode HTML entities in P开发者_如何学编程ython string? (6 answers) Closed 7 years ago.
print u'<'

How can I print <

print '>' 

How can I print &gt;


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜