python 如何将“>”更改为“>” 和 '>' '>'?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1977900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:27:42  来源:igfitidea点击:

How can I change '>' to '>' and '>' to '>'?

python

提问by zjm1126

print u'<'

How can I print <

我怎样才能打印 <

print '>' 

How can I print &gt;

我怎样才能打印 &gt;

回答by jbochi

You should use HTMLParsermodule to decode html:

您应该使用HTMLParsermodule 来解码 html

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

To escape HTML, cgi module is fine:

要转义 HTML,cgi 模块很好

>>> cgi.escape(u'<a>bá</a>').encode('ascii', 'xmlcharrefreplace')
'&lt;a&gt;b&#225;&lt;/a&gt;