如何转换& lt;进入<在lxml中,Python吗? [英] How to convert < into < in lxml, Python?

查看:166
本文介绍了如何转换& lt;进入<在lxml中,Python吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个xml文件:

<body>
    <entry>
         I go to <hw>to</hw> to school.
    </entry>
</body>

由于某种原因,在使用lxml解析器进行解析之前,我将<hw>更改为&lt;hw&gt;,将</hw>更改为&lt;/hw&gt;.

For some reason, I changed <hw> to &lt;hw&gt; and </hw> to &lt;/hw&gt; before parsing it with lxml parser.

<body>
    <entry>
         I go to &lt;hw&gt;to&lt;/hw&gt; to school.
    </entry>
</body>

但是在修改了已解析的xml数据之后,我想要一个<hw>元素,而不是&lt;hw&gt;.我该怎么办?

But after modifying the parsed xml data, I want to get a <hw> element, not &lt;hw&gt;. How can I do that?

推荐答案

首先找到一个unescape函数:

from xml.sax.saxutils import unescape

entry=body[0]

取消转义并将其替换为原始文件:

unescape and replace it with the original:

body.replace(entry, e.fromstring(unescape(e.tounicode(entry))))

这篇关于如何转换&amp; lt;进入&lt;在lxml中,Python吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆