zoukankan      html  css  js  c++  java
  • Python解析XMl

    import xml.etree.ElementTree as ET
    tree = ET.parse("xml_test")
    root = tree.getroot()
    # print(root.tag)#打印根标签
    
    # #遍历xml文档
    # for child in root:
    #     print(child.tag,child.attrib)
    #     for i in child:
    #         print(i.tag,i.text)
    
    
    # 修改
    # for node in root.iter('price'):
    #     new_price = node.text
    #     print(new_price)
    #     node.text = "¥"+str(new_price)
    #     node.set("updated","yes")
    # tree.write("xml_test")
    
    #删除
    for node in root.findall('food'):
        kind = str(node.find('name').text).startswith('S')
        print(kind)
        if kind:
            root.remove(node)
    tree.write("xml_test")
    <breakfast_menu>
        <food>
            <name>Belgian Waffles</name>
            <price>$5.95</price>
            <description>
            two of our famous Belgian Waffles with plenty of real maple syrup
            </description>
            <calories>650</calories>
        </food>
        <food>
            <name>Berry-Berry Belgian Waffles</name>
            <price>$8.95</price>
            <description>
            light Belgian waffles covered with an assortment of fresh berries and whipped cream
            </description>
            <calories>900</calories>
        </food>
        <food>
            <name>French Toast</name>
            <price>$4.50</price>
            <description>
            thick slices made from our homemade sourdough bread
            </description>
            <calories>600</calories>
        </food>
        <food>
            <name>Homestyle Breakfast</name>
            <price>$6.95</price>
            <description>
            two eggs, bacon or sausage, toast, and our ever-popular hash browns
            </description>
            <calories>950</calories>
        </food>
    </breakfast_menu>
  • 相关阅读:
    数据的艺术
    第十七篇 make的路径搜索综合实践
    第十六篇 make中的路径搜索
    第十五篇 make中的隐式规则概述
    第十四篇 自动生成依赖关系(终结)
    [SDOI2009]HH的项链解题报告
    欧几里德与扩展欧几里德算法的理解、实现与应用
    浅析强连通分量(Tarjan和kosaraju)
    deque-at
    plt.imshow()
  • 原文地址:https://www.cnblogs.com/softtester/p/11649604.html
Copyright © 2011-2022 走看看