【问题标题】:how to read xml in python with a specific tag attribute?如何在 python 中读取具有特定标签属性的 xml?
【发布时间】:2018-01-19 02:27:36
【问题描述】:

我有一个 xml 标签,我在 python 中使用下面的代码来读取这个标签,但我只想在 type = "Actual" 时获取数据,否则忽略。目前我得到了他们每个人的价值,即 40,50,60。

xml 标签:

 <student type="Actual">40</student>
 <student type="estimated">50</student>
 <student>60</student>

Python 代码:

student = root.find("./student")

请您告知此语法所需的更改。

【问题讨论】:

  • student = root.find("./student&gt; 应该是 Python 还是 XML?
  • 这是一个python代码

标签: python xml elementtree


【解决方案1】:

您可以使用以下语法列出具有“实际类型”属性的所有节点:

students = root.findall('./student[@type="Actual"]')

【讨论】:

  • 我需要一个尾随 / 来完成这项工作:root.findall('./student[@type="Actual"]/')
猜你喜欢
  • 1970-01-01
  • 2016-02-24
  • 2013-07-01
  • 2020-05-02
  • 1970-01-01
  • 1970-01-01
  • 2015-03-11
  • 1970-01-01
  • 2016-04-30
相关资源
最近更新 更多