python - 根据属性值使用 lxml 对子元素进行排序

标签 python xml sorting lxml

我正在尝试根据属性值对文档中的某些子元素进行排序,而实际的排序功能似乎有效,而新排序的元素的拼接似乎无效。

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/

相关文章:

python - 如何使用 Tkinter 导入索引列为 "date"的 csv 文件

python - 如何用一个循环依次遍历多个列表?

python - 数据帧到字符串

java - Hibernate无法创建表

.net - System.Xml.Linq.XDocument' 在未引用的程序集中定义

c# - 使用 xsi :schemaLocation namespace 创建 XDocument

java - 我怎样才能找到数组中三个最低整数的位置?

python - 如何优雅地解决Python KeyError(Python csv库)

javascript - slice() 方法的问题,数组显然是自动排序的,但实际上并没有

javascript如何在按其属性排序后获取对象的顺序