Python搜索孩子的特定孩子

时间:2018-10-01 14:26:30

标签: python xml parsing search

我正在尝试下载xml数据(已完成),解析它(已完成)并搜索xml数据以返回结果。

substance='Substance 2'
product_name='Product XYZ'

for mrl in root.findall(substance):
    for prod in find(product):
        print(mrl.text)

在本地保存xml文件后(因为它很大),我想搜索一种物质,例如“ Substance 2”,然后搜索产品名称“ Product XYZ”,然后搜索MRL数据根据该产品特定于该物质。

(请注意,物质都是唯一的,但产品不是唯一的,因为产品会在每种不同的物质下重复出现)

为了更加清楚,我将举一个例子:如果物质搜索词为“物质2”,而产品搜索词为“ Product XYZ”,程序将返回“ 0.567”。

[编辑] 到目前为止,我与该问题有关的代码部分为:

{{1}}

这不会引发错误,但也不会输出任何输出。

[\ edit]

请让我知道是否需要更多详细信息。帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

使用本教程https://docs.python.org/3/library/xml.etree.elementtree.html

for substance in root.find('Substances')
  if substance.find("Name").text = "Substance 2":
    for product in substance.findall('Product'):
        if  product.find('Product_name').text == 'Product XYZ':
            return product.find('MRL').text
相关问题