本文介绍了按标签用python对xml进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 xml
<root>
<node1>
<B>text</B>
<A>another_text</A>
<C>one_more_text</C>
</node1>
<node2>
<C>one_more_text</C>
<B>text</B>
<A>another_text</A>
</node2>
</root>
我想得到如下输出:
<root>
<node1>
<A>another_text</A>
<B>text</B>
<C>one_more_text</C>
</node1>
<node2>
<A>another_text</A>
<B>text</B>
<C>one_more_text</C>
</node2>
</root>
我尝试了一些代码,例如:
I tried with some code like:
from xml.etree import ElementTree as et
tr = et.parse(path_in)
root = tr.getroot()
for children in root.getchildren():
for child in children.getchildren():
# sort it
tr.write(path_out)
我不能使用标准函数 sort
和 sorted
因为它排序错误(不是按标签).提前致谢.
I cannot use standard function sort
and sorted
because it sorted wrong way (not by tag).Thanks in advance.
推荐答案
您需要:
- 获取每个顶级节点"的子元素
- 按
tag
属性(节点名称) - 重置每个顶级节点的子节点
示例工作代码:
from operator import attrgetter
from xml.etree import ElementTree as et
data = """ <root>
<node1>
<B>text</B>
<A>another_text</A>
<C>one_more_text</C>
</node1>
<node2>
<C>one_more_text</C>
<B>text</B>
<A>another_text</A>
</node2>
</root>"""
root = et.fromstring(data)
for node in root.findall("*"): # searching top-level nodes only: node1, node2 ...
node[:] = sorted(node, key=attrgetter("tag"))
print(et.tostring(root))
打印:
<root>
<node1>
<A>another_text</A>
<B>text</B>
<C>one_more_text</C>
</node1>
<node2>
<A>another_text</A>
<B>text</B>
<C>one_more_text</C>
</node2>
</root>
请注意,我们没有使用 getchildren()
方法 在这里(它实际上是 deprecated 自 Python 2.7) - 使用每个 Element
实例是一个可迭代的子节点.
Note that we are not using getchildren()
method here (it is actually deprecated since Python 2.7) - using the fact that each Element
instance is an iterable over the child nodes.
这篇关于按标签用python对xml进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!