lxml.etree将元素插入到element.text中 [英] lxml.etree insert elements into element.text

查看:132
本文介绍了lxml.etree将元素插入到element.text中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的字符串中包含空的xml元素,如下所示:

I have strings that have empty xml elements in them, like this:

>>> s = """fizz buzz <pb n="44"/> bananas"""

这些字符串已使用etree.SubElement方法分配给xml元素:

These strings have been assigned to xml elements using the etree.SubElement method:

>>> from lxml import etree as et
>>> root = et.Element('root')
>>> txt = et.SubElement(root, 'text')
>>> txt.text = s
>>> et.dump(root)
<root>
  <text>fizz buzz &lt;pb n="44"/&gt; bananas</text>
</root>

re.split()和etree的texttail上摆弄一下,我可以在txt.text中需要的地方插入一个子元素<pb n="44"/>;但是,有时我在字符串中多次出现<pb/>元素,这使事情变得复杂:

Fiddling about with re.split() and etree's text and tail I can insert a subelement <pb n="44"/> where I want it in txt.text; however, sometimes I've got multiple occurrences of the <pb/> element in the string, which complicates matters:

>>> s1 = """foo bar <pb n="42"/> parrots like <pb n="43"/> eggs and spam"""

是否有一种简单的方法将这些元素插入到它们属于现有元素text的位置,而又不会在texttail上摆弄太多?

Is there a straightforward way to insert such elements where they belong in an existing element's text without fiddling around too much with text and tail?

推荐答案

您可以使输入字符串成为格式正确的XML文档(以text作为根元素),然后使用fromstring()将其解析为Element对象.然后将其附加到父项.

You could make your input string a well-formed XML document (with text as the root element) and parse that into an Element object using fromstring(). Then append it to the parent.

from lxml import etree as et

s1 = """foo bar <pb n="42"/> parrots like <pb n="43"/> eggs and spam"""
s2 = "<text>{0}</text>".format(s1)

text = et.fromstring(s2)
root = et.Element('root')
root.append(text)

et.dump(root)

输出:

<root>
 <text>foo bar <pb n="42"/> parrots like <pb n="43"/> eggs and spam</text>
</root>

这篇关于lxml.etree将元素插入到element.text中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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