Redundante Zeitinformationen aus XML-Datei löschen
Verfasst: Donnerstag 17. Januar 2019, 15:45
				
				Hallo!
Gerne möchte ich eine XML-Datei so bereinigen, dass doppelte Einträge gelöscht werden. Etwas Code habe ich schon geschrieben. Ich bräuchte noch Hinweise, wie man ein XML-Element löscht.
Viele liebe Grüße, Strawk
			Gerne möchte ich eine XML-Datei so bereinigen, dass doppelte Einträge gelöscht werden. Etwas Code habe ich schon geschrieben. Ich bräuchte noch Hinweise, wie man ein XML-Element löscht.
Code: Alles auswählen
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 16 16:02:50 2019
@author: Admin
"""
import xml.etree.ElementTree as ET
def delete_equal_elements():
    """
    deletes redundant time information in GPX(XML)-file
    """
    tree = ET.parse('XML_files/Myway_B.xml')
    root = tree.getroot()
    
    timeList = []
    for child in root:
        for e1 in child:
            for e2 in e1:
                for e3 in e2:
                    eleTime = str(e3.text)
                    # print(time)
                    if eleTime[10:] != '':
                        timeList.append(eleTime[10:])
                    
    countEqualTimes = 0
    for i in range(0, len(timeList)-1):
        if timeList[i] == timeList[i+1]:
            countEqualTimes += 1
            root.remove # !!!!!!! what must be in this row? !!!!!!!
        
    print(countEqualTimes)
    
if __name__ == "__main__":
    delete_equal_elements()