2 回答
TA贡献1880条经验 获得超4个赞
其中一项优化不会像您现在使用tree.xpath('//*')if 语句那样遍历所有标签并进行检查。这可以替换为tree.xpath('//Type')
接下来需要优化的是迭代值。Value您无需一遍又一遍地迭代( tree.xpath('//Value')),您可以获得标签的所有同级Values标签Typeelem.xpath('./following-sibling::Value')
from lxml import etree
xml_file = open('stack_sample.xml')
tree = etree.parse(xml_file)
root = tree.getroot()
for elem in tree.xpath('//Type'):
_id = elem.getparent().attrib["Id"]
_type = elem.text
_position = elem.attrib["pos"]
values = elem.xpath('./following-sibling::Value')
for value in values:
_object = value.attrib['Object']
_result = value.xpath(f'./result[@pos={_position}]/text()')[0]
print(_id, _type, _object, _result)
这将打印出:
Packages totalPackages total 1200
DeliveryMethod packagesSent priority 100
DeliveryMethod packagesSent express 200
DeliveryMethod packagesSent ground 300
DeliveryMethod packagesReceived priority 100
DeliveryMethod packagesReceived express 200
DeliveryMethod packagesReceived ground 300
编辑
这是针对特定情况的解决方案,其中我们确定resultinValue标签的数量等于与其他解决方案Type是同级的标签的数量Value,另外解决方案假设Type和result按相同pos属性排序。
请记住,这是一种非常具体的解决方案,而不是通用的解决方案。
from lxml import etree
xml_file = open('stack_sample.xml')
tree = etree.parse(xml_file)
root = tree.getroot()
for elem in tree.xpath('//Type'):
_id = elem.getparent().attrib["Id"]
_type = elem.text
_objects = elem.xpath('./following-sibling::Value/@Object')
_results = elem.xpath('./following-sibling::Value/result/text()')
for _object, _result in zip(_objects, _results):
print(_id, _type, _object, _result)
输出:
Packages totalPackages total 1200
DeliveryMethod packagesSent priority 100
DeliveryMethod packagesSent express 100
DeliveryMethod packagesSent ground 200
DeliveryMethod packagesReceived priority 100
DeliveryMethod packagesReceived express 100
DeliveryMethod packagesReceived ground 200
TA贡献1842条经验 获得超21个赞
//*如果您不迭代所有标签( ),而只是迭代,也许性能会更高<Value>:
from lxml import etree
xml_file = open('stack_sample.xml')
tree = etree.parse(xml_file)
root = tree.getroot()
for val in tree.xpath('//Value'):
t = {t.get('pos'): t.text for t in val.getparent().xpath('./Type')}
for r in val.xpath('./result'):
print(val.getparent().get('Id'), val.get('Object'), t[r.get('pos')], r.text)
印刷:
Packages total totalPackages 1200
DeliveryMethod priority packagesSent 100
DeliveryMethod priority packagesReceived 100
DeliveryMethod express packagesSent 200
DeliveryMethod express packagesReceived 200
DeliveryMethod ground packagesSent 300
DeliveryMethod ground packagesReceived 300
添加回答
举报