Using XPath in ElementTree

后端 未结 5 1438
情话喂你
情话喂你 2020-11-30 00:51

My XML file looks like the following:




        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-30 00:59

    from xml.etree import ElementTree as ET
    tree = ET.parse("output.xml")
    namespace = tree.getroot().tag[1:].split("}")[0]
    amount = tree.find(".//{%s}Amount" % namespace).text
    

    Also, consider using lxml. It's way faster.

    from lxml import ElementTree as ET
    

提交回复
热议问题