【问题标题】:How can I change '>' to '>' and '>' to '>'? [duplicate]如何将 '>' 更改为 '>' 和 '>' 更改为 '>'? [复制]
【发布时间】:2010-12-30 23:28:18
【问题描述】:
print u'<'

如何打印<

print '>' 

如何打印>

【问题讨论】:

  • 我不使用 Python,但那些被称为“HTML 实体”。如果您在 Google 中使用该新关键字四处寻找,您可能会找到足够的信息:google.com/search?q=python+html+entities
  • 这可能还不够,BalusC。 OP 在他的个人资料中明确表示,英文解释很难理解,他想要 code考虑到问题是代码的形式
  • 是的,BalusC,把 c0dez 发给他

标签: python


【解决方案1】:

你应该使用HTMLParser模块来decode html

>>> import HTMLParser
>>> h= HTMLParser.HTMLParser()
>>> h.unescape('alpha < β')
u'alpha < \u03b2'

要转义 HTML,cgi module is fine:

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

【讨论】:

  • +1 用于 HTMLParser 和出色的链接。
猜你喜欢
  • 2018-02-11
  • 2018-01-20
  • 2020-01-11
  • 1970-01-01
  • 2021-04-25
  • 2014-06-02
  • 2019-06-30
  • 2011-09-07
  • 2013-10-13
相关资源
最近更新 更多