【问题标题】:How do make a list in which the subelements are joined in (Python Element tree)如何制作一个包含子元素的列表(Python元素树)
【发布时间】:2017-11-17 03:40:12
【问题描述】:

我有这个示例 XML 代码

<pathway>
    <relation entry1="62" entry2="64" type="PPrel">
        <subtype name="activation" value="--&gt;"/>
    </relation>
    <relation entry1="54" entry2="55" type="PPrel">
        <subtype name="activation" value="--&gt;"/>
        <subtype name="phosphorylation" value="+p"/>
    </relation>
    <relation entry1="55" entry2="82" type="PPrel">
        <subtype name="activation" value="--&gt;"/>
        <subtype name="phosphorylation" value="+p"/>
    </relation>
</pathway>

我正在尝试将子类型排序到一个列表中,但是如果条目有多个子类型,则将它们组合成一个字符串

示例输出: ['激活', '激活;磷酸化','活化;磷酸化']

我当前的代码是

tree= ET.parse('file.xml')
root= tree.getroot()
relation = []
for son in root:
    for step_son in son:
        if len(son.getchildren()) > 1:
            relation.append(step_son.get('name'))
        if len(son.getchildren()) < 2:
            relation.append(step_son.get('name'))

我的关系输出是:

['活化', '活化', '磷酸化', '活化', 磷酸化']

任何帮助都会很棒,谢谢!

【问题讨论】:

    标签: python list elementtree


    【解决方案1】:

    使用查找和迭代每个匹配元素:

    In [35]: from xml.etree import ElementTree
    In [36]: xml_string = """
        ...: <pathway>
        ...:     <relation entry1="62" entry2="64" type="PPrel">
        ...:         <subtype name="activation" value="--&gt;"/>
        ...:     </relation>
        ...:     <relation entry1="54" entry2="55" type="PPrel">
        ...:         <subtype name="activation" value="--&gt;"/>
        ...:         <subtype name="phosphorylation" value="+p"/>
        ...:     </relation>
        ...:     <relation entry1="55" entry2="82" type="PPrel">
        ...:         <subtype name="activation" value="--&gt;"/>
        ...:         <subtype name="phosphorylation" value="+p"/>
        ...:     </relation>
        ...: </pathway>"""
    
    In [37]: p_element = ElementTree.fromstring(xml_string)
    
    In [38]: result = []
    
    In [39]: for relation in p_element.findall('.//relation'):
        ...:    result.append(';'.join(x.attrib['name'] for x in relation.findall('.//subtype')))
        ...:
    
    In [40]: result
    Out[40]: ['activation', 'activation;phosphorylation', 'activation;phosphorylation']
    

    【讨论】:

    • 非常感谢,感谢您的帮助。
    猜你喜欢
    • 2020-10-20
    • 2022-11-14
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    • 2016-07-16
    • 2022-08-19
    • 2021-12-13
    • 2012-04-27
    相关资源
    最近更新 更多