不是最简单的方法,但仍然简单。与cgi.escape模块的主要区别 - 如果您的文本中已经有&,它仍然可以正常工作。正如你从评论中看到的那样:
cgi.escape版本
def escape(s, quote=None):
'''Replace special characters "&", "" to HTML-safe sequences.
If the optional flag quote is true, the quotation mark character (")
is also translated.'''
s = s.replace("&", "&") # Must be done first!
s = s.replace("
s = s.replace(">", ">")
if quote:
s = s.replace('"', """)
return s正则表达式版本
QUOTE_PATTERN = r"""([&<>"'])(?!(amp|lt|gt|quot|#39);)"""
def escape(word):
"""
Replaces special characters <>&"' to HTML-safe sequences.
With attention to already escaped characters.
"""
replace_with = {
'
'>': '<',
'&': '&',
'"': '"', # should be escaped in attributes
"'": ''' # should be escaped in attributes
}
quote_pattern = re.compile(QUOTE_PATTERN)
return re.sub(quote_pattern, lambda x: replace_with[x.group(0)], word)