在Python中转义特殊的HTML字符 [英] Escape special HTML characters in Python

查看:85
本文介绍了在Python中转义特殊的HTML字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,其中包含特殊字符,例如' & (...)可能会出现。

I have a string where special characters like ' or " or & (...) can appear. In the string:

string = """ Hello "XYZ" this 'is' a test & so on """

我如何自动转义每个特殊字符,以便得到以下内容:

how can I automatically escape every special character, so that I get this:

string = " Hello "XYZ" this 'is' a test & so on "


推荐答案

在Python 3.2中,您可以使用 html.escape 函数,例如

In Python 3.2, you could use the html.escape function, e.g.

>>> string = """ Hello "XYZ" this 'is' a test & so on """
>>> import html
>>> html.escape(string)
' Hello "XYZ" this 'is' a test & so on '

对于早期版本的Python,请检查 http://wiki.python.org/moin/EscapingHtml

For earlier versions of Python, check http://wiki.python.org/moin/EscapingHtml:


<$ Python随附的c $ c> cgi 模块具有 escape()函数

import cgi

s = cgi.escape( """& < >""" )   # s = "&amp; &lt; &gt;"

但是,它不会转义以外的字符; < > 。如果用作 cgi.escape(string_to_escape,quote = True),它还会转义

However, it doesn't escape characters beyond &, <, and >. If it is used as cgi.escape(string_to_escape, quote=True), it also escapes ".

以下是一个小片段,您也可以使用引号和撇号:

Here's a small snippet that will let you escape quotes and apostrophes as well:

 html_escape_table = {
     "&": "&amp;",
     '"': "&quot;",
     "'": "&apos;",
     ">": "&gt;",
     "<": "&lt;",
     }

 def html_escape(text):
     """Produce entities within text."""
     return "".join(html_escape_table.get(c,c) for c in text)






您也可以使用 escape() xml.sax.saxutils 中的c $ c>转义html,此函数的执行速度应更快。 unescape()


You can also use escape() from xml.sax.saxutils to escape html. This function should execute faster. The unescape() function of the same module can be passed the same arguments to decode a string.

from xml.sax.saxutils import escape, unescape
# escape() and unescape() takes care of &, < and >.
html_escape_table = {
    '"': "&quot;",
    "'": "&apos;"
}
html_unescape_table = {v:k for k, v in html_escape_table.items()}

def html_escape(text):
    return escape(text, html_escape_table)

def html_unescape(text):
    return unescape(text, html_unescape_table)


这篇关于在Python中转义特殊的HTML字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆