如何在python etree中正确地转义XML?

编程入门 行业动态 更新时间:2024-10-28 05:22:58
本文介绍了如何在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 &lt;StackOverflow&gt; 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.

如您所见,解析器必须将&lt;更改为<等.

As you can see, the parser must have changed the &lt;'s to <'s etc.

我想看的东西

The tag &lt;StackOverflow&gt; 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 &lt;StackOverflow&gt; 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 &lt;StackOverflow&gt; is good to bring up at parties.

更多推荐

如何在python etree中正确地转义XML?

本文发布于:2023-11-17 03:57:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608748.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确地   如何在   etree   python   XML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!