简体   繁体   中英

remove element from elementtree/list

I'm editing tags with elementtree/lists, and after I get information from config tags I want to delete the tag. I try to do so below with i.remove(j) and if I loop through the list of lists I can see that indeed that config tag is removed. However when i write out to the file they are still there, why is this, how do I remove them? Is it that I am editing a sublist and then writing a different list to the file?

import xml.etree.ElementTree as ET
ET.register_namespace("", "http://clish.sourceforge.net/XMLSchema")
tree = ET.parse('ethernet.xml')
root = tree.getroot()

command= ""
pattern = ""
operation = ""
priority= ""

action_command = """/klas/klish-scripts/ifrange.py run_command --cmdrange "${interface_method} ${iface_num} ${range_separator} ${iface_num2}  ${range_separator2} ${interface_method2} ${iface_num3} ${range_separator3} ${iface_num4} ${range_separator4} ${interface_method3} ${iface_num5} ${range_separator5} ${iface_num6} ${range_separator6} ${interface_method4} ${iface_num7} ${range_separator7} ${iface_num8}" --command "%s" --klish_config "%s" --klish_action "%s" --priority "%s"
"""

commands = root.findall('{http://clish.sourceforge.net/XMLSchema}'
                        'VIEW/{http://clish.sourceforge.net/XMLSchema}COMMAND')
all1 = []

for command in commands:
    all1.append(list(command.iter()))

atr = ""
for i in all1:
    for j in i:
        if "COMMAND" in j.tag:
            if "name" in j.attrib:
                pattern = j.attrib['name']
                #print operation
        if "CONFIG" in j.tag:
            if "operation" in j.attrib:
                operation = j.attrib['operation']  
            else:
                operation = "set"  
            if "pattern" in j.attrib:
                pattern = j.attrib['pattern']                       
            if "priority" in j.attrib:
                priority = j.attrib['priority']
            else:
                if operation == "unset":
                    priority = ""
                else:
                    priority = "0x7f00"    

            atr = str(j.attrib)
            **i.remove(j)**

        if "ACTION" in j.tag: 
            if j.text:
                command = j.text.strip()
                j.text= action_command % (command, pattern, operation, priority)
            else:
                command = ""                        

cmd = ""
cmd += ifrange  


for o in all1:
    for y in o:
        print y
    **cmd += ET.tostring(o[0], encoding="utf-8", method="xml")**
cmd += end_tags


f = open('Z2.xml', 'w')
f.write(cmd)
f.close

EDIT: solution, at the end of the file before I write to the file I reset all1 to []. I then loop through the tree removing the necessary elements.

all1 = []
for command in commands:
    for i in command:
         #print i
         if "CONFIG" in str(i):
             command.remove(i)
    all1.append(list(command.iter()))

You are only removing references to the elements from your list. You need to call .remove() on the parent element instead. ElementTree does not retain parent pointers; given a CONFIG element alone you cannot go back the VIEW element that is its parent.

This means you need to retain a reference to the parent as well. Loop over the VIEW elements, then in a nested loop find the CONFIG elements you want to remove and with the VIEW parent still available, call .remove() to remove a child element from that parent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM