Home > Software engineering >  TypeError: remove() argument must be xml.etree.ElementTree.Element, not dict
TypeError: remove() argument must be xml.etree.ElementTree.Element, not dict

Time:06-28

How to remove specific indexing attribute

For example : if you look at below xml data from that need to remove the name attribute which at first indexing -> get_root_element[0]

XML data :

<?xml version='1.0' encoding='utf-8'?>
<data>
  <row>
    <index>0</index>
    <price>$5.95</price>
    <name>Belgian Waffles</name>
    <desc>Two of our famous Belgian Waffles with plenty of real maple syrup</desc>
    <calories>650</calories>    
  </row>
  <row>
    <index>1</index>
    <price>$7.95</price>
    <name>Strawberry Belgian Waffles</name>
    <desc>Light Belgian waffles covered with strawberries and whipped cream</desc>
    <calories>900</calories>
  </row>
  <row>
    <index>2</index>
    <price>$8.95</price>
    <name>Berry-Berry Belgian Waffles</name>
    <desc>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</desc>
    <calories>900</calories>
  </row>
  <row>
    <index>3</index>
    <price>$4.50</price>
    <name>French Toast</name>
    <desc>Thick slices made from our homemade sourdough bread</desc>
    <calories>600</calories>
  </row>
  <row>
    <index>4</index>
    <price>$6.95</price>
    <name>Homestyle Breakfast</name>
    <desc>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</desc>
    <calories>950</calories>
  </row>
</data>

My code :

import xml.etree.ElementTree as ET

parse_xml = ET.parse('/content/sample_data/xyz.xml')
get_root_element = parse_xml.getroot()
get_root_element[0].remove( get_root_element[0].attrib )
parse_xml.write('/content/sample_data/demo.xml')

Need to remove only first indexing <row> </row> within that the <name> .. </name>

Expected data

<?xml version='1.0' encoding='utf-8'?>
<data>
  <row>
    <index>0</index>
    <price>$5.95</price>
    <desc>Two of our famous Belgian Waffles with plenty of real maple syrup</desc>
    <calories>650</calories>    
  </row>
  <row>
    <index>1</index>
    <price>$7.95</price>
    <name>Strawberry Belgian Waffles</name>
    <desc>Light Belgian waffles covered with strawberries and whipped cream</desc>
    <calories>900</calories>
  </row>
  <row>
    <index>2</index>
    <price>$8.95</price>
    <name>Berry-Berry Belgian Waffles</name>
    <desc>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</desc>
    <calories>900</calories>
  </row>
  <row>
    <index>3</index>
    <price>$4.50</price>
    <name>French Toast</name>
    <desc>Thick slices made from our homemade sourdough bread</desc>
    <calories>600</calories>
  </row>
  <row>
    <index>4</index>
    <price>$6.95</price>
    <name>Homestyle Breakfast</name>
    <desc>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</desc>
    <calories>950</calories>
  </row>
</data>

CodePudding user response:

Finally I was able to solve it

import xml.etree.ElementTree as ET

parse_xml = ET.parse('/content/sample_data/xyz.xml')
get_root_element = parse_xml.getroot()

for idx,data in enumerate(get_root_element):
  if idx == 0:
    for header in data:
      if header.tag == "name" :
        data.remove(header)
      else:
        pass
  else:
    continue

parse_xml.write('/content/sample_data/abc.xml')

CodePudding user response:

Here's another option that is, in my opinion, a bit easier to read/understand.

import xml.etree.ElementTree as ET

parsed_xml = ET.parse("/content/sample_data/xyz.xml")

first_row = parsed_xml.find("row[1]")
name_elem = first_row.find("name")
first_row.remove(name_elem)

parsed_xml.write("/content/sample_data/abc.xml")
  • Related