Edit XML file text based on path

后端 未结 3 457
遇见更好的自我
遇见更好的自我 2020-12-19 01:56

I have an XML file (e.g. jerry.xml) which contains some data as given below.



    2<         


        
3条回答
  •  忘掉有多难
    2020-12-19 02:47

    You should be able to use the XPath capabilities of the module to do this:

    import xml.etree.ElementTree as ET
    tree = ET.parse('jerry.xml')
    root = tree.getroot()
    for data in root.findall(".//country[@name='singapore']/gdpnp[@month='08']"):
        data.text = csv_value
    
    tree.write("filename.xml")
    

    So you need to rewrite the path in the csv to match the XPath rules defined for the module (see Supported XPath rules).

提交回复
热议问题