本文介绍了如何在python etree中正确地转义XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用python 2.7.3版本.
I'm using python version 2.7.3.
test.txt:
<?xml version="1.0" encoding="UTF-8"?> <root> <test>The tag <StackOverflow> is good to bring up at parties.</test> </root>结果:
>>> import xml.etree.ElementTree as ET >>> e = ET.parse('test.txt') >>> root = e.getroot() >>> print root.find('test').text The tag <StackOverflow> is good to bring up at parties.如您所见,解析器必须将<更改为<等.
As you can see, the parser must have changed the <'s to <'s etc.
我想看的东西
The tag <StackOverflow> is good to bring up at parties.
未修饰的原始文本.有时候我真的很喜欢它.未煮熟.
Untouched, raw text. Sometimes I really like it raw. Uncooked.
我想按原样使用此文本在HTML中显示,因此我不希望XML解析器将其弄乱.
I'd like to use this text as-is for display within HTML, therefore I don't want an XML parser to mess with it.
我是否必须重新转义每个字符串,或者还有其他方法吗?
Do I have to re-escape each string or can there be another way?
推荐答案import xml.etree.ElementTree as ET e = ET.parse('test.txt') root = e.getroot() print(ET.tostring(root.find('test')))
收益
<test>The tag <StackOverflow> is good to bring up at parties.</test>或者,您可以使用 saxutils.escape :
import xml.sax.saxutils as saxutils print(saxutils.escape(root.find('test').text))收益
The tag <StackOverflow> is good to bring up at parties.更多推荐
如何在python etree中正确地转义XML?
发布评论