我正在尝试根据属性值对文档中的某些子元素进行排序,而实际的排序功能似乎有效,而新排序的元素的拼接似乎无效。
from lxml import etree
def getkey(elem):
# Used for sorting elements by @LIN.
# returns a tuple of ints from the exploded @LIN value
# '1.0' -> (1,0)
# '1.0.1' -> (1,0,1)
return tuple([int(x) for x in elem.get('LIN').split('.')])
xml_str = """<Interface>
<Header></Header>
<PurchaseOrder>
<LineItems>
<Line LIN="2.0"></Line>
<Line LIN="3.0"></Line>
<Line LIN="1.0"></Line>
</LineItems>
</PurchaseOrder>
</Interface>"""
root = etree.fromstring(xml_str)
lines = root.findall("PurchaseOrder/LineItems/Line")
lines[:] = sorted(lines, key=getkey)
res_lines = [x.get('LIN') for x in lines]
print res_lines
print etree.tostring(root, pretty_print=True)
当我执行上面的代码时,我会看到 lines
列表在打印 ['1.0', '2.0', '3.0']
时正确排序。然而,XML 树并未更新,因为 tostring() 打印出以下内容。
<Interface>
<Header/>
<PurchaseOrder>
<LineItems>
<Line LIN="2.0"/>
<Line LIN="3.0"/>
<Line LIN="1.0"/>
</LineItems>
</PurchaseOrder>
</Interface>
我想到了如何从 http://effbot.org/zone/element-sort.htm 排序,它说拼接应该是我更新元素顺序所需的全部,但情况似乎并非如此。我意识到 lxml 与 elementtree 并非 100% 兼容,因此作为完整性检查,我将 lxml 导入替换为 elementtree 并获得了完全相同的结果。
最佳答案
这将排序并写入输出:
import xml.etree.ElementTree as ET
tree = ET.parse("in.xml")
def getkey(elem):
# Used for sorting elements by @LIN.
# returns a tuple of ints from the exploded @LIN value
# '1.0' -> (1,0)
# '1.0.1' -> (1,0,1)
return float(elem.get('LIN'))
container = tree.find("PurchaseOrder/LineItems")
container[:] = sorted(container, key=getkey)
tree.write("new.xml")
或者使用自己的代码打印:
import xml.etree.ElementTree as ET
tree = ET.fromstring(xml_str)
def getkey(elem):
# Used for sorting elements by @LIN.
# returns a tuple of ints from the exploded @LIN value
# '1.0' -> (1,0)
# '1.0.1' -> (1,0,1)
return float(elem.get('LIN'))
root = etree.fromstring(xml_str)
lines = root.find("PurchaseOrder/LineItems")
lines[:] = sorted(lines, key=getkey)
输出:
In [12]: print (etree.tostring(root, pretty_print=True))
<Interface>
<Header/>
<PurchaseOrder>
<LineItems>
<Line LIN="1.0"/>
<Line LIN="2.0"/>
<Line LIN="3.0"/>
</LineItems>
</PurchaseOrder>
</Interface>
关键是 root.find("PurchaseOrder/LineItems")
,您想要找到 LineItems
元素并对其进行排序。
关于python - 根据属性值使用 lxml 对子元素进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36018627/