lxml findall和逆序

时间:2011-10-16 14:38:31

标签: python lxml

如何以相反的顺序打印所有findall元素?

labels = tree.findall(label)

for label in labels:
     print etree.tostring(label)

2 个答案:

答案 0 :(得分:3)

有一个reversed功能:

for label in reversed(labels)

答案 1 :(得分:1)

或者可能是这样的:

labels = ET.findall(label)
alist =[]
for one_label in labels:
     alist.append(etree.tostring(one_label))

print sorted(alist, reverse=True)