我有一个字符串,其中可以出现 '
或 "
或 &
(...) 等特殊字符。在字符串中:
string = """ Hello "XYZ" this 'is' a test & so on """
我怎样才能自动转义每个特殊字符,以便我得到这个:
string = " Hello "XYZ" this 'is' a test & so on "
最佳答案
在 Python 3.2 中,您可以使用 html.escape
function ,例如
>>> 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 :
The
cgi
module that comes with Python has anescape()
function:import cgi s = cgi.escape( """& < >""" ) # s = "& < >"
However, it doesn't escape characters beyond
&
,<
, and>
. If it is used ascgi.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)
You can also use
escape()
fromxml.sax.saxutils
to escape html. This function should execute faster. Theunescape()
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 - 在 Python 中转义特殊的 HTML 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2077283/