Moh*_*nka 8 python celementtree
对于xml
<grandparent>
<parent1>
<child>data1</child>
</parent1>
<parent2>
<child>data2</child>
</parent2>
</grandparent>
Run Code Online (Sandbox Code Playgroud)
我需要包含父元组的列表,xml中每个父元素的数据.
有没有办法使用cElementTree?我能够为孩子,数据做这件事,但不幸的是孩子在所有的价值观上是相同的,因此没有多少用处.
似乎您可以使用 ElementTree 的 1.3 版(检查http://effbot.org/zone/element-xpath.htm)通过使用 xpath 命令(如 child.find('../parent'). 但我认为 python 附带 1.2 版或其他版本。
您还应该检查与 etree 兼容并具有完整 Xpath 支持的 lxml http://lxml.de/
parent_map = dict((c, p) for p in tree.getiterator() for c in p)
parent_map[el].remove(el)
Run Code Online (Sandbox Code Playgroud)