问题
I have a string that have both XML escaped characters and non-escaped, and I need it to be 100% XML valid, example:
>>> s = '< <'
I want this to be:
>>> s = '< <'
I have tried numerous methods with, lxml, cgi etc.. but they all expect the input string to not have any valid XML characters already:
>>> import cgi
>>> cgi.escape("< <")
'< &lt;'
or
>>> from xml.sax.saxutils import escape
>>> escape("< <")
'< &lt;'
Isn't there a standard method for this already?
Someone has to have had the same problem :)
回答1:
Your best bet is to unescape, then to re-escape:
>>> from xml.sax.saxutils import escape, unescape
>>> unescape("< <")
'< <'
>>> escape(unescape("< <"))
'< <'
来源:https://stackoverflow.com/questions/25247885/static-python-method-to-xml-escape-string-which-supports-quotes