0

我对 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'。Jobs 与 people 是不同类型的对象,因为它们都是从返回的列表中的项目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 的人员的姓名。

4

0 回答 0