问题描述
我有一个字符串,其中包含特殊字符,例如'
或
或&
(...)可能会出现。
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中,您可以使用,例如
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,请检查:
For earlier versions of Python, check http://wiki.python.org/moin/EscapingHtml:
import cgi
s = cgi.escape( """& < >""" ) # s = "& < >"
但是,它不会转义以外的字符;
,<
和>
。如果用作 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 = {
"&": "&",
'"': """,
"'": "'",
">": ">",
"<": "<",
}
def html_escape(text):
"""Produce entities within text."""
return "".join(html_escape_table.get(c,c) for c in text)
您也可以使用中的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 = {
'"': """,
"'": "'"
}
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字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!