我怎样才能用一种更简单的方式来写呢?

2024-09-28 05:28:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有几行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(),但没有成功


Tags: nameinidforgetxmlreactnotes
1条回答
网友
1楼 · 发布于 2024-09-28 05:28:49

另一种方法

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

相关问题 更多 >

    热门问题