SimpleDao
Python xml,html转义字符
2019-05-06, 访问数: 924

xml转义字符

  1. from xml.sax.saxutils import escape
  2. def xmlescape(data):
  3. if data is None:
  4. data = ''
  5. elif not isinstance(data, str):
  6. data = str(data)
  7. return escape(data, entities={"'": "'", '"': """})

html转义字符

  1. def html_escape(s):
  2. htmlcodes = (
  3. ('&', '&'),
  4. ("'", '''),
  5. ('"', '"'),
  6. ('>', '>'),
  7. ('<', '&lt;'),
  8. )
  9. for code in htmlcodes:
  10. s = s.replace(code[0], code[1])
  11. return s