python lxml添加保留所有父树的子元素

时间:2019-01-28 10:26:26

标签: python python-3.x lxml

我有这样的东西:

<source>Some text <g>that is like this.<g> Needs </g></g>. <g> I want to keep this structure</g></source>

我想添加一个标签以保持此结果:

<source><en>Some text <g>that is like this.<g> Needs </g></g>. <g> I want to keep this structure</g></en></source>

但是,追加,插入或子元素都不会保留父级的结构。

from lxml import etree
xml = et.fromstring('<source>Some text <g>that is like this.<g> Needs </g></g>. <g> I want to keep this structure</g></source>')
root = xml.getroot()
element = et.Element('en')
root.insert(0, element)
print(et.tostring(root))

但是它会插入一个空标签。我不知道如何保持父级的整个结构这样:

<source><en />Some text <g>that is like this.<g> Needs </g></g>. <g> I want to keep this structure</g></source>

2 个答案:

答案 0 :(得分:3)

您可以重命名原始的根元素,然后将其附加到新的根元素。

from lxml import etree

old_root = etree.fromstring('<source>Some text <g>that is like this.<g> Needs </g></g>. <g> I want to keep this structure</g></source>')

old_root.tag = "en"
new_root = etree.Element("source")
new_root.append(old_root)

print(etree.tostring(new_root))

结果:

<source><en>Some text <g>that is like this.<g> Needs </g></g>. <g> I want to keep this structure</g></en></source>

答案 1 :(得分:0)

这是我的解决方案。希望这会有所帮助。

enter image description here

干杯!