我对Elementtrees如何在Python中工作感到有点困惑,因为下面的代码不起作用:
import xml.etree.ElementTree as ET
file = ("People.xml")
tree = parse(file)
myList = tree.findall("person")
for people in myList:
print people.attrib["name"]
jobs = people.findall("jobs/job")
for job in jobs:
print job.attrib["title"]
我收到错误AttributeError 'list' object has no attribute 'attrib'
。作业是一种不同于人的对象类型,因为它们都是从findall
返回的列表中的项目?
示例xml文件是:
<person name="p1">
<address>address1</address>
<jobs>
<job title="title1">job1</job>
<job title="title2" pay="pay1">job2</job>
</jobs>
</person>
<person name="p2">
<address>address2</address>
<jobs>
<job title="title3">job3</job>
<job title="title4" pay="pay2">job4</job>
</jobs>
</person>
我需要搜索我正在尝试的性质的原因是我正在尝试打印其工作“pay”属性设置为pay2的人的姓名。