Python 如果缺少属性,则跳过XML标记

Python 如果缺少属性,则跳过XML标记,python,xml,Python,Xml,我试图从下面的XML文件中获取数据。对于每种类型,说明应在其旁边 例如: 橙色它们属于柑橘类,在低于摄氏度的温度下不能生长 柠檬它们属于柑橘类植物。它们不能在低于摄氏度的温度下生长 <Fruits> <Fruit> <Family>Citrus</Family> <Explanation>They belong to the Citrus.They cannot grow at a tempera

我试图从下面的XML文件中获取数据。对于每种类型,说明应在其旁边

例如:

橙色它们属于柑橘类,在低于摄氏度的温度下不能生长 柠檬它们属于柑橘类植物。它们不能在低于摄氏度的温度下生长

<Fruits>
    <Fruit>
        <Family>Citrus</Family>
        <Explanation>They belong to the Citrus.They cannot grow at a temperature below</Explanation>
        <Type>Orange</Type>
        <Type>Lemon</Type>
        <Type>Lime</Type>
        <Type>Grapefruit</Type>
    </Fruit>
        <Fruit>
        <Family>Pomes</Family>
        <Type>Apple</Type>
        <Type>Pear</Type>        
    </Fruit>
</Fruits>
如果属性解释缺失,我怎么能跳过像水果族(Pomes)这样的标记呢?

使用xml.etree,只需尝试查找解释子项:

您还可以使用xpath,在xpath中,只有当水果节点具有解释子节点时,才可以使用:

如果我们在您的样品上运行,您将看到我们只得到柑橘:

In [1]: xml = """<Fruits>
   ...:     <Fruit>
   ...:         <Family>Citrus</Family>
   ...:         <Explanation>They belong to the Citrus.They cannot grow at a temperature below</Explanation>
   ...:         <Type>Orange</Type>
   ...:         <Type>Lemon</Type>
   ...:         <Type>Lime</Type>
   ...:         <Type>Grapefruit</Type>
   ...:     </Fruit>
   ...:         <Fruit>
   ...:         <Family>Pomes</Family>
   ...:         <Type>Apple</Type>
   ...:         <Type>Pear</Type>
   ...:     </Fruit>
   ...: </Fruits>"""


In [2]: import lxml.etree as et

In [3]: root = et.fromstring(xml)

In [4]: for node in root.xpath("//Fruit[Explanation]"):
   ...:         print(node.xpath("Family/text()"))
   ...:     
['Citrus']
[1]中的
:xml=“”
...:     
…:柑橘
…:它们属于柑橘类植物,在低于摄氏度的温度下不能生长
…:橙色
…:柠檬
…:石灰
…:葡萄柚
...:     
...:         
…:柚子
…苹果
…:梨
...:     
...: """
在[2]中:将lxml.etree作为et导入
在[3]中:root=et.fromstring(xml)
在[4]中:对于root.xpath(“//Fruit[解释]”)中的节点:
…:打印(node.xpath(“Family/text()”)
...:     
[“柑橘”]
Nice解决方案(+1)。专业提示:
//水果[./解释]
//水果[解释]
是等效的。
from  xml.etree import ElementTree as et
root = et.fromstring(xml)

for node in root.iter("Fruit"):
    if node.find("Explanation") is not None:
        print(node.find("Family").text)
import lxml.etree as et

root = et.fromstring(xml)

for node in root.xpath("//Fruit[Explanation]"):
     print(node.xpath("Family/text()"))
In [1]: xml = """<Fruits>
   ...:     <Fruit>
   ...:         <Family>Citrus</Family>
   ...:         <Explanation>They belong to the Citrus.They cannot grow at a temperature below</Explanation>
   ...:         <Type>Orange</Type>
   ...:         <Type>Lemon</Type>
   ...:         <Type>Lime</Type>
   ...:         <Type>Grapefruit</Type>
   ...:     </Fruit>
   ...:         <Fruit>
   ...:         <Family>Pomes</Family>
   ...:         <Type>Apple</Type>
   ...:         <Type>Pear</Type>
   ...:     </Fruit>
   ...: </Fruits>"""


In [2]: import lxml.etree as et

In [3]: root = et.fromstring(xml)

In [4]: for node in root.xpath("//Fruit[Explanation]"):
   ...:         print(node.xpath("Family/text()"))
   ...:     
['Citrus']