我有这样的东西:
<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>
答案 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)