[英]TypeError: remove() argument must be xml.etree.ElementTree.Element, not str | python | xml.etree
[英]TypeError: remove() argument must be xml.etree.ElementTree.Element, not dict
如何删除特定的索引属性
例如:如果您查看下面的 xml 数据,则需要删除首先索引的name
属性-> get_root_element[0]
XML 数据:
<?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>
我的代码:
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')
只需要删除<name> .. </name>
中的第一个索引<row> </row>
>
预期数据
<?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>
这是另一种选择,在我看来,它更容易阅读/理解。
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")
最后我能够解决它
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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.