gpt4 book ai didi

python - 我怎样才能以更简单的方式写这个?

转载 作者:行者123 更新时间:2023-12-04 10:34:17 25 4
gpt4 key购买 nike

我有一些 XML 行,我需要从中解析和提取 react 和产品列表的物种名称,到目前为止,我已尝试使用以下几行,但我想知道是否有办法更清楚地做到这一点

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 代码,我可以解析和提取物种的名称,但输出是一个没有区分 react 和产物的列表,我也尝试过 element.iter() 但没有成功

最佳答案

另一种方法。

from simplified_scrapy import SimplifiedDoc,utils
html = utils.getFileContent('example.xml')
doc = SimplifiedDoc(html)
reactions = doc.listOfReactions.reactions
for reaction in reactions:
print (reaction['id'],reaction['name']) # to get the list of REACTIONS ids and names
# gives back the list of species for reactants and products
print ('Reactants')
print (reaction.selects('listOfReactants>speciesReference>species()'))
print ('Products')
print (reaction.selects('listOfProducts>speciesReference>species()'))
结果:
ID_1 name_1
Reactants
['react_1_1', 'react_2_1', 'react_3_1']
Products
['produ_1_1', 'produ_2_1', 'produ_3_1']
ID_2 name_2
Reactants
['react_1_2']
Products
['produ_1_2']
ID_3 name_3
Reactants
['react_1_3', 'react_2_3']
Products
['produ_1_3', 'produ_2_3']
以下是更多示例: https://github.com/yiyedata/simplified-scrapy-demo/tree/master/doc_examples

关于python - 我怎样才能以更简单的方式写这个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60264326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com