在 xml.etree.ElementTree、Python 中获取 "child" 的 "sibling",
Getting the "sibling" of a "child" in xml.etree.ElementTree, Python,
我是 XML 和 python 的新手,我正在努力获得 "child"
的 "sibling"
我有这个XML
<notes>
<Def id="1">
<module>DDAC</module>
<tags> lalala</tags>
<description> John and Mark are good friends. </description>
</Def>
<Def id="2">
<module>FYP</module>
<tags> lelele</tags>
<description> John works in Google. </description>
</Def>
<Def id="3">
<module>FYP</module>
<tags> lilili</tags>
<description> Mark work in IBM. </description>
</Def>
<Def id="4">
<module>DDAC</module>
<tags> lololo</tags>
<description> A computer can help you do stuff </description>
</Def>
<Def id="5">
<module>IMNPD</module>
<tags> lululu</tags>
<description> The internet is a world wide web </description>
</Def>
</notes>
我想获取所有模块的描述"DDAC"
for g in root.iter('module'):
if g.text == 'DDAC':
x = root.iter("description")
print(x)
我的期望输出是:
John and Mark are good friends.
A computer can help you do stuff
但我得到的是对象而不是文本
假设您的 xml 数据在名为 test.xml 的文件中,以下代码应该有效:
import xml.etree.ElementTree as ET
root = ET.parse('test.xml').getroot()
for Def in root.findall('Def'):
module = Def.find('module').text
if module == 'DDAC':
print(Def.find('description').text)
我是 XML 和 python 的新手,我正在努力获得 "child"
的 "sibling"我有这个XML
<notes>
<Def id="1">
<module>DDAC</module>
<tags> lalala</tags>
<description> John and Mark are good friends. </description>
</Def>
<Def id="2">
<module>FYP</module>
<tags> lelele</tags>
<description> John works in Google. </description>
</Def>
<Def id="3">
<module>FYP</module>
<tags> lilili</tags>
<description> Mark work in IBM. </description>
</Def>
<Def id="4">
<module>DDAC</module>
<tags> lololo</tags>
<description> A computer can help you do stuff </description>
</Def>
<Def id="5">
<module>IMNPD</module>
<tags> lululu</tags>
<description> The internet is a world wide web </description>
</Def>
</notes>
我想获取所有模块的描述"DDAC"
for g in root.iter('module'):
if g.text == 'DDAC':
x = root.iter("description")
print(x)
我的期望输出是:
John and Mark are good friends.
A computer can help you do stuff
但我得到的是对象而不是文本
假设您的 xml 数据在名为 test.xml 的文件中,以下代码应该有效:
import xml.etree.ElementTree as ET
root = ET.parse('test.xml').getroot()
for Def in root.findall('Def'):
module = Def.find('module').text
if module == 'DDAC':
print(Def.find('description').text)