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 :)
Your best bet is to unescape, then to re-escape:
>>> from xml.sax.saxutils import escape, unescape
>>> unescape("< <")
'< <'
>>> escape(unescape("< <"))
'< <'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With