You are not logged in.

Announcement

*** NOTICE: forum.openstreetmap.org is being retired. Please request a category for your community in the new ones as soon as possible using this process, which will allow you to propose your community moderators.
Please create new topics on the new site at community.openstreetmap.org. We expect the migration of data will take a few weeks, you can follow its progress here.***

#1 2021-07-30 19:24:24

Shohreh
Member
Registered: 2012-12-15
Posts: 247

[Python+lxml] Remove nodes with specific name?

Hello,

I'm sure there are people here who know Python and lxml.

I want to remove all occurences of the "time" nodes from a GPX file.

This does nothing:

import lxml.etree as et

tree = et.parse("input.gpx")

for elem in tree.xpath( '//time' ) :
	elem.getparent().remove(elem)

with open("output.gpx", 'wb') as doc:
   doc.write(et.tostring(tree, pretty_print = True)) 

What's the right way?

Thank you.

---
Edit: For others' benefit — although I'm still pretty much in the dark when it comes to XML parsers:

#AttributeError: 'xml.etree.ElementTree.Element' object has no attribute 'getparent'
#import xml.etree.ElementTree as ET
import lxml.etree as ET

tree = ET.parse('input.gpx')
#Must prepend namespace; There might be a way to tell ET to search while ignoring it
ns = {"sm": "http://www.topografix.com/GPX/1/1"}

for elem in tree.findall(".//sm:time", ns):
	#print(elem.text)
	elem.getparent().remove(elem)
	
with open("removed.time.gpx", 'wb') as doc:
   doc.write(ET.tostring(tree, pretty_print = True))

Last edited by Shohreh (2021-07-31 12:36:07)

Offline

Board footer

Powered by FluxBB