【问题标题】:Is there a standard library way to convert text to HTML entities in Python 2.7?在 Python 2.7 中是否有标准库方法可以将文本转换为 HTML 实体?
【发布时间】:2019-02-24 03:25:05
【问题描述】:

我想输入需要转换为 HTML 实体的文本并输出 HTML 实体。

输入:

x < 7 && I like math && y > 7

输出:

x &lt; 7 &amp;&amp; I like math &amp;&amp; y &gt; 7

我可以使用字符串操作手动执行此替换,但我想知道是否有标准库方法可以在自己滚动之前执行此操作。

【问题讨论】:

标签: python html python-2.7 html-entities


【解决方案1】:

xml.sax.saxutils 库是标准的,可以在 Python 2.7 中转义和取消转义 xml

from xml.sax.saxutils import escape, unescape

mystr = escape('x < 7 && I like math && y > 7')
print(mystr)
# x &lt; 7 &amp;&amp; I like math &amp;&amp; y &gt; 7
print(unescape(mystr))
# x < 7 && I like math && y > 7

【讨论】:

    猜你喜欢
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-20
    • 2018-02-21
    • 2012-05-24
    • 1970-01-01
    相关资源
    最近更新 更多