实现删除元素树元素的功能

huangapple go评论52阅读模式
英文:

Implementing remove function for element tree element

问题

for country in root.findall('country'):
    description_element = country.find('description')
    if description_element is not None:
        country.remove(description_element)

tree.write('SampleData.xml')
英文:

I am trying to remove an element from an XML tree. My attempt is based on the example that I found in the Python documentation:

> python
> for country in root.findall('country'):
> # using root.findall() to avoid removal during traversal
> rank = int(country.find('rank').text)
> if rank > 50:
> root.remove(country)
>
> tree.write('output.xml')
>

But I'm trying to use the remove() function for a string attribute, not an integer one.

for country in root.findall('country'):
    # using root.findall() to avoid removal during traversal
    description = country.find('rank').text
    root.remove(description)
    
tree.write('SampleData.xml')

But I get the following error:

> TypeError: remove() argument must be xml.etree.ElementTree.Element, not str.

I ultimately added another element under country called description which holds a short description of the country and its features:

<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
        <description>Liechtenstein has a lot of flowers.</description>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N" />
        <description>Singapore has a lot of street markets</description>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W" />
        <neighbor name="Colombia" direction="E" />
        <description>Panama is predominantly Spanish speaking.</description>
    </country>
</data>

I'm trying to use the remove() function to delete that description attribute for all instances.

答案1

得分: 1

在你的代码中,你传递的是一个字符串作为参数,而不是一个节点:

    description = country.find('rank').text
    root.remove(description)

在正确的示例中,使用整数的情况如下:

    rank = int(country.find('rank').text)
    if rank > 50:
        root.remove(country)

请注意,country 被移除(一个节点),而不是 rank(一个整数)。

不清楚你想要对描述做什么处理,但确保移除的是一个节点,而不是一个字符串。例如,

    description = country.find('rank').text
    if description == "delete this":  # 仅为示例条件
        root.remove(country)

或者,如果你只想移除 "rank" 节点并保留国家节点:

    ranknode = country.find('rank')
    if ranknode.text == "delete this":
        country.remove(ranknode)

正如你提到的,你实际上有一个 description 元素(你称其为属性,但这很令人困惑),你可以定位元素,而不是 rank

    descriptionnode = country.find('description')
    if descriptionnode.text == "delete this":
        country.remove(descriptionnode)
英文:

Indeed, in your code you are passing a string as argument, not a node:

    description = country.find('rank').text
    root.remove(description)

This is not what happens in the correct examples. The one with the integer does this:

    rank = int(country.find('rank').text)
    if rank > 50:
        root.remove(country)

Note that country is removed (a node), not rank (an int).

It is not clear what you want to do with the description, but make sure to remove a node, not a string. For instance,

    description = country.find('rank').text
    if description == "delete this":  # just an example condition
        root.remove(country)

Or, if you just want to remove the "rank" node and keep the country node:

    ranknode = country.find('rank')
    if ranknode.text == "delete this":
        country.remove(ranknode)

As you mention you have actually a description element (you call it an attribute, but that is confusing), you can target that element instead of rank:

    descriptionnode = country.find('description')
    if descriptionnode.text == "delete this":
        country.remove(descriptionnode)

huangapple
  • 本文由 发表于 2023年2月10日 05:05:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404417.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定