[英]Getting the “sibling” of a “child” in xml.etree.ElementTree, Python,
I am new to XML and python, I am struggling to get the "sibling" of a "child"我是 XML 和 python 的新手,我正在努力获得“孩子”的“兄弟姐妹”
I have this XML我有这个 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>
I want to get the description for all the module "DDAC"我想获得所有模块“DDAC”的描述
for g in root.iter('module'):
if g.text == 'DDAC':
x = root.iter("description")
print(x)
my desire output is:我的愿望输出是:
John and Mark are good friends.
约翰和马克是好朋友。
A computer can help you do stuff
电脑可以帮你做事
but I am getting the object not the text但我得到的对象不是文本
Assuming your xml data is in a file called test.xml, the following code should work:假设您的 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.