Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.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 2.7 xml.etree。结亲_Python_Xml_Python 2.7_Xml Parsing - Fatal编程技术网

Python 2.7 xml.etree。结亲

Python 2.7 xml.etree。结亲,python,xml,python-2.7,xml-parsing,Python,Xml,Python 2.7,Xml Parsing,我得到的xml的结构类似于下面的示例 [...] <a> <list> <section> <identifier root="88844433"></templateId> <code code="6664.2" display=&

我得到的xml的结构类似于下面的示例

            [...]
            <a>
            <list>
            <section>
                <identifier root="88844433"></templateId>
                <code code="6664.2" display="Relevant"></code>
                <title>Section title </title>
            </section>
            </a>
            </list>
            [...]
[…]
章节标题 [...] 如何在Python2.7中使用xml.etree通过标识符块的根属性搜索标题块?

如下

import xml.etree.ElementTree as ET

xml = '''   <a>
            <list>
            <section>
                <templateId root="12"></templateId>
                <code code="6664.2" display="Relevant"></code>
                <title>Section title </title>
            </section>
            </list>
            </a>'''

root = ET.fromstring(xml)
section = root.find(".//section/templateId[@root='12']/..")
print(section.find('title').text)
Section title