How do I get properly escaped XML in python etree untouched?

泄露秘密 提交于 2019-12-01 19:27:40
import xml.etree.ElementTree as ET
e = ET.parse('test.txt')
root = e.getroot()
print(ET.tostring(root.find('test')))

yields

<test>The tag &lt;StackOverflow&gt; is good to bring up at parties.</test>

Alternatively, you could escape the text with saxutils.escape:

import xml.sax.saxutils as saxutils
print(saxutils.escape(root.find('test').text))

yields

The tag &lt;StackOverflow&gt; is good to bring up at parties.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!