Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python ElementTree查找包含特定文本的“title”xml标记的父元素_Python_Xml_Elementtree - Fatal编程技术网

使用Python ElementTree查找包含特定文本的“title”xml标记的父元素

使用Python ElementTree查找包含特定文本的“title”xml标记的父元素,python,xml,elementtree,Python,Xml,Elementtree,我希望解析一个xml文件,并使用Python3.7&ElementTree提取包含匹配特定文本的父文件 ... <sec id="s0010"> <label>2</label> <title>Materials and methods</title> </sec> <sec id="s0015"> <label>3</label&

我希望解析一个xml文件,并使用Python3.7&ElementTree提取包含匹配特定文本的父文件

    ...
    <sec id="s0010">
     <label>2</label>
     <title>Materials and methods</title>
     </sec>
    <sec id="s0015">
     <label>3</label>
     <title>Summary</title>
     </sec>

     ...

但是如何获取包含感兴趣文本的标题的父对象呢?

分两步进行嵌套迭代:在秒上,然后在标题上。比如:

对于parent.itersec中的sec: 对于第i节中的标题: text=title.text 如果text.lower中有文本和方法: 打印**标题:+text+*****秒id:+sec.getid,
有关更多详细信息,请检查。

您已经有了父项。是否需要?好的,还有一个比较,比较字符串是什么?否。。我希望找到感谢!就是这样。。只需关闭打印括号;是的,对不起,我很忙,没有通过解释器运行代码。
for title in parent.iter('title'):
                        text = title.text
                        if(text):
                                if("methods" in text.lower()):
                                        print("**title: "+text+"****")