This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author WoodyWoo
Recipients WoodyWoo, docs@python
Date 2020-10-01.01:11:46
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1601514706.89.0.342670385975.issue41899@roundup.psfhosted.org>
In-reply-to
Content
My fault.
"for country in root.findall('country')“ is not working as same as "for country in root"
all 3 method below:

import xml.etree.ElementTree as ET 
xmlstr=\
r'''<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama1">
        <rank updated="yes">69</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
    <country name="Panama2">
        <rank updated="yes">69</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>'''
print(xmlstr)

#orginal code
root = ET.fromstring(xmlstr)
for country in root.findall('country'):
    rank = int(country.find('rank').text)
    if rank > 50:
        root.remove(country)
print("___orginal___")
for country in root.findall('country'):
        print (country.get("name"))
print("^^^orginal^^^^\n")

#wrong code in my mind
root = ET.fromstring(xmlstr)
for country in root:
    rank = int(country.find('rank').text)
    if rank > 50:
        root.remove(country)
print("___bad___")
for country in root.findall('country'):
        print (country.get("name"))
print("^^^bad^^^^\n")

#my code
root = ET.fromstring(xmlstr)
index=0
count=len(root.findall("./*"))
while index <count:
    rank = int (root[index].find("rank").text)
    if rank>50:
        root.remove(root[index])
        index=index+0
        count=count-1  # avoid index err
        continue
    index=index+1
print("___new___")
for country in root.findall('country'):
        print (country.get("name"))
print("^^^new^^^^\n")
History
Date User Action Args
2020-10-01 01:11:46WoodyWoosetrecipients: + WoodyWoo, docs@python
2020-10-01 01:11:46WoodyWoosetmessageid: <1601514706.89.0.342670385975.issue41899@roundup.psfhosted.org>
2020-10-01 01:11:46WoodyWoolinkissue41899 messages
2020-10-01 01:11:46WoodyWoocreate