Re-Combine Elements from root.findall(".//") in Python

73 views Asked by At

I'm parsing BytesIO SVG data as an Element Tree. I start with:

 tree = ET.parse(svg)
 root = tree.getroot()

Where root is:

 <Element '{http://www.w3.org/2000/svg}svg' at 0x000001E19B2E54E0>

My goal is to use findall() and manipulate the resultant list of elements:

 for elem in root.findall(".//"):
    
      # manipulate elements

Where root.findall(".//") gives:

 [<Element '{http://www.w3.org/2000/svg}metadata' at 0x000001E19B2E5710>, <Element '{http://www.w3.org/1999/02/22-rdf-syntax-ns#}RDF' at 0x000001E19B2E51C0>, <Element '{http://creativecommons.org/ns#}Work' at 0x000001E19B2E5670>, ...]

With a list of new elements, I want to put it back into the original root format so that I can save a new root to the svg:

 # Somehow get new_root = <Element '{http://www.w3.org/2000/svg}svg' at 0x000001E19B2E54E0>

 ElementTree(new_root).write(svg)
 svg.seek(0)
 

How would this last part be accomplished? Thanks

0

There are 0 answers