【发布时间】:2017-11-17 03:40:12
【问题描述】:
我有这个示例 XML 代码
<pathway>
<relation entry1="62" entry2="64" type="PPrel">
<subtype name="activation" value="-->"/>
</relation>
<relation entry1="54" entry2="55" type="PPrel">
<subtype name="activation" value="-->"/>
<subtype name="phosphorylation" value="+p"/>
</relation>
<relation entry1="55" entry2="82" type="PPrel">
<subtype name="activation" value="-->"/>
<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