我有几行XML,我需要从中解析和提取反应和产物列表中的物种名称,到目前为止,我已经尝试了以下几行,但我想知道是否有一种方法可以更清楚地做到这一点
XML:
<?xml version="1.0" encoding="UTF-8"?>
<sbml xmlns="data" level="2" version="1">
<model id="E" name="core_model">
<notes>
<listOfUnitDefinitions>
<listOfCompartments>
<listOfSpecies>
<listOfReactions>
<reaction id="ID_1" name="name_1">
<notes>
<listOfReactants>
<speciesReference species="react_1_1"/>
<speciesReference species="react_2_1"/>
<speciesReference species="react_3_1"/>
</listOfReactants>
<listOfProducts>
<speciesReference species="produ_1_1"/>
<speciesReference species="produ_2_1"/>
<speciesReference species="produ_3_1"/>
</listOfProducts>
<kineticLaw>
</reaction>
<reaction id="ID_2" name="name_2">
<notes>
<listOfReactants>
<speciesReference species="react_1_2"/>
</listOfReactants>
<listOfProducts>
<speciesReference species="produ_1_2"/>
</listOfProducts>
<kineticLaw>
</reaction>
<reaction id="ID_3" name="name_3">
<notes>
<listOfReactants>
<speciesReference species="react_1_3"/>
<speciesReference species="react_2_3"/>
</listOfReactants>
<listOfProducts>
<speciesReference species="produ_1_3"/>
<speciesReference species="produ_2_3"/>
</listOfProducts>
<kineticLaw>
</reaction>
</listOfReactions>
</model>
</sbml>
Python:
import xml.etree.ElementTree as et
tree = et.parse('example.xml')
root = tree.getroot()
child = root[0]
for x in child[4]: #to get the list of REACTIONS ids and names
print (x.get('id'),':',x.get('name'))
for h in range(2): #gives back the list of species for reactants and products
for i in range(2):
for x in child[4][h][i+1]:
print(x.get('species'))
印刷品:
react_1_1
react_2_1
react_3_1
produ_1_1
produ_2_1
produ_3_1
react_1_2
produ_1_2
期望输出
ID_1
Reactants
react_1_1
react_2_1
react_3_1
Products
produ_1_1
produ_2_1
produ_3_1
ID_2
Reactions
react_1_2
Products
produ_1_2
.
.
.
使用python代码,我可以解析和提取物种的名称,但输出是一个列表,不区分反应和产物,我也尝试过使用element.iter(),但没有成功
另一种方法
结果:
这里有更多的例子:https://github.com/yiyedata/simplified-scrapy-demo/tree/master/doc_examples
相关问题 更多 >
编程相关推荐