Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static python method to XML escape string which supports quotes

Tags:

python

xml

lxml

I have a string that have both XML escaped characters and non-escaped, and I need it to be 100% XML valid, example:

>>> s = '< &lt;'

I want this to be:

>>> s = '&lt; &lt;'

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;")
'&lt; &amp;lt;'

or

>>> from xml.sax.saxutils import escape
>>> escape("< &lt;")
'&lt; &amp;lt;'

Isn't there a standard method for this already?

Someone has to have had the same problem :)

like image 325
Niklas9 Avatar asked Mar 19 '23 18:03

Niklas9


1 Answers

Your best bet is to unescape, then to re-escape:

>>> from xml.sax.saxutils import escape, unescape
>>> unescape("< &lt;")
'< <'
>>> escape(unescape("< &lt;"))
'&lt; &lt;'
like image 67
Martijn Pieters Avatar answered Apr 08 '23 17:04

Martijn Pieters