Static python method to XML escape string which supports quotes

▼魔方 西西 提交于 2019-12-22 10:45:24

问题


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 :)


回答1:


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

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


来源:https://stackoverflow.com/questions/25247885/static-python-method-to-xml-escape-string-which-supports-quotes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!