面对一个可迭代对象,如何进行选定呢?
如果不想记,这个可迭代对象的专用语法,有两种通用方法:
方法一:list(可迭代对象)转化为列表
这个使用print(list(可迭代对象)[index])访问特定的对象
适用范围:可迭代对象的数据量不大,内存可以容纳下
举个例子:
# 需求:把下面content里根标签下第二个country节点 的属性打印出来
content = """
<data>
<country name="Liechtenstein">
<rank>2</rank>
<year>2023</year>
<gdppc>141100</gdppc>
<neighbor direction="E" name="Austria" />
<neighbor direction="W" name="Switzerland" />
</country>
<country name="Panama">
<rank>69</rank>
<year>2026</year>
<gdppc>13600</gdppc>
<neighbor direction="W" name="Costa Rica" />
<neighbor direction="E" name="Colombia" />
</country>
</data>
"""
原本的方法是使用findall变成列表,现在只需要直接list
from xml.etree import ElementTree as ET
content = """
<data>
<country name="Liechtenstein">
<rank>2</rank>
<year>2023</year>
<gdppc>141100</gdppc>
<neighbor direction="E" name="Austria" />
<neighbor direction="W" name="Switzerland" />
</country>
<country name="Panama">
<rank>69</rank>
<year>2026</year>
<gdppc>13600</gdppc>
<neighbor direction="W" name="Costa Rica" />
<neighbor direction="E" name="Colombia" />
</country>
</data>
"""
root = ET.XML(content)
# 原先的办法
print(root.findall('country')[1].attrib)
# 使用list通法
print(list(root)[1].attrib)
方法二:使用iter转化为生成器进行迭代
from xml.etree import ElementTree as ET
content = """
<data>
<country name="Liechtenstein">
<rank>2</rank>
<year>2023</year>
<gdppc>141100</gdppc>
<neighbor direction="E" name="Austria" />
<neighbor direction="W" name="Switzerland" />
</country>
<country name="Panama">
<rank>69</rank>
<year>2026</year>
<gdppc>13600</gdppc>
<neighbor direction="W" name="Costa Rica" />
<neighbor direction="E" name="Colombia" />
</country>
</data>
"""
root = ET.XML(content)
# 由于root是个可迭代对象,所以先转化为生成器
root_gen = iter(root)
# 由于我们要访问第二个country,所以循环两次
for count in range(2):
sub_root = next(root_gen)
# 输出第二个country的属性
print(sub_root.attrib)