Home > Mobile >  How to remove closing tag from an xml file in python?
How to remove closing tag from an xml file in python?

Time:11-26

From this:

<trip id="2404" from="84823308" to="-33789567" depart="0.00">
    </trip>

I would like to get this:

<trip id="2404" from="84823308" to="-33789567" depart="0.00"/>

I know these two lines are not equivalent, but I was wondering if there was a way to do this.


EDIT

This was enough to solve the problem

    tree = ET.parse(input_filename)
    root = tree.getroot()
    for trip in root.findall(".//trip"):
        trip.text = ''
    tree.write(output_filename, encoding="utf-8",  method="xml", xml_declaration=True)

CodePudding user response:

This was enough to solve the problem

 tree = ET.parse(input_filename)
    root = tree.getroot()
    for trip in root.findall(".//trip"):
        trip.text = ''
    tree.write(output_filename, encoding="utf-8",  method="xml", xml_declaration=True)
  • Related