You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python ElementTree处理XML:保留指定lane并删除无效edge

Python ElementTree 筛选XML节点解决方案

问题场景

我是XML编程新手,用Python ElementTree开发,现有如下XML结构:

<net>
    <edge id=":1006232713_w0" function="walkingarea">
        <lane id=":1006232713_w0_0" index="0" allow="pedestrian" speed="1.00" />
        <lane id=":1006232713_w0_1" index="0" disallow="pedestrian" speed="1.00"/>      
    </edge>
    <edge id=":1006237429_0" function="internal">
        <lane id=":1006237429_0_0" index="0" allow="delivery bicycle" speed="5.69"/>
    </edge>
    <edge id=":1006237429_1" function="internal">
        <lane id=":1006237429_1_0" index="0" allow="pedestrian" speed="3.65"/>
    </edge>
    <edge id=":1006237429_w0" function="walkingarea">
        <lane id=":1006237429_w0_0" index="0" allow="pedestrian" speed="1.00"/>
        <lane id=":1006237429_w0_0" index="0" disallow="pedestrian" speed="5.50"/>
    </edge>
    <edge id=":1006249156_w0" function="walkingarea">
        <lane id=":1006249156_w0_0" index="0" allow="pedestrian" speed="1.00"/>
    </edge>
    <edge id=":1006249161_w0" function="walkingarea">
        <lane id=":1006249161_w0_0" index="0" disallow="pedestrian" speed="1.00"/>
    </edge>
        
</net>

需求:

  • 保留带有allow="pedestrian"属性的lane节点,删除其他lane节点
  • 若edge节点下无符合条件的lane,则删除该edge及其所有子节点

我尝试了以下代码,但它会输出edge下所有lane的id,达不到预期:

for edge in root.findall("./edge/lane/[@allow= 'pedestrian']..."):
    for lane in edge.find("./lane/[@allow= 'pedestrian']..."):
        print(lane.attrib['id'])

解决方案

可以通过遍历每个edge节点,先筛选保留符合条件的lane,再判断是否需要保留该edge,具体代码如下:

import xml.etree.ElementTree as ET

# 加载XML文件
tree = ET.parse('your_xml_file.xml')
root = tree.getroot()

# 遍历所有edge节点(转成列表避免遍历过程中修改结构引发异常)
for edge in list(root.findall('edge')):
    # 收集当前edge下符合条件的lane节点
    valid_lanes = edge.findall("./lane[@allow='pedestrian']")
    # 收集当前edge下不符合条件的lane节点
    invalid_lanes = [lane for lane in edge.findall('lane') if lane not in valid_lanes]
    
    # 删除所有无效lane
    for lane in invalid_lanes:
        edge.remove(lane)
    
    # 若edge下无剩余lane,删除该edge
    if len(edge.findall('lane')) == 0:
        root.remove(edge)

# 保存处理后的XML
tree.write('filtered_net.xml', encoding='utf-8', xml_declaration=True)

代码说明

  1. 遍历edge节点:用list(root.findall('edge'))将节点转为列表,防止遍历过程中修改XML结构导致的异常
  2. 筛选有效lane:通过XPath表达式.//lane[@allow='pedestrian']直接定位符合条件的lane节点
  3. 删除无效lane:找出所有不在有效列表中的lane,逐个从edge节点中移除
  4. 清理空edge:检查edge下剩余的lane数量,若为0则从根节点删除该edge
  5. 保存结果:将处理后的XML写入文件,保留UTF-8编码和XML声明

处理后的XML将符合预期输出:

<net>

    <edge id=":1006232713_w0" function="walkingarea">
        <lane id=":1006232713_w0_0" index="0" allow="pedestrian" speed="1.00" />        
    </edge>

    <edge id=":1006237429_w0" function="walkingarea">
        <lane id=":1006237429_w0_0" index="0" allow="pedestrian" speed="1.00"/>
    </edge>
    <edge id=":1006249156_w0" function="walkingarea">
        <lane id=":1006249156_w0_0" index="0" allow="pedestrian" speed="1.00"/>
    </edge>
    
</net>

内容的提问来源于stack exchange,提问作者Thilaga

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 17:30:46