支持引号的XML转义字符串的静态python方法 [英] Static python method to XML escape string which supports quotes
本文介绍了支持引号的XML转义字符串的静态python方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个同时包含XML转义字符和非转义字符的字符串,我需要它是100%XML有效的,例如:
I have a string that have both XML escaped characters and non-escaped, and I need it to be 100% XML valid, example:
>>> s = '< <'
我希望这样:
>>> s = '< <'
我用lxml,cgi等尝试了许多方法,但是他们都希望输入字符串已经没有任何有效的XML字符:
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;'
或
>>> 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("< <"))
'< <'
这篇关于支持引号的XML转义字符串的静态python方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文