Python:子属性满足条件时提取XML元素值

Python: Extract XML element value when children attribute meet criteria

我是 XML 解析的初学者 当子属性满足某些条件时,我无法提取特定值。

这是我的 xml 文件的示例(来自 http://www.uniprot.org/uniprot/Q63HN8.xml):

<uniprot xmlns="http://uniprot.org/uniprot" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://uniprot.org/uniprot http://www.uniprot.org/support/docs/uniprot.xsd">
  <entry dataset="Swiss-Prot" created="2007-02-20" modified="2015-09-16" version="112">
    <accession>Q63HN8</accession>
    <accession>C9JCP4</accession>
    <accession>D6RI12</accession>
  <dbReference type="Proteomes" id="UP000005640">
    <property type="component" value="Chromosome 17"/>
  </dbReference>
  <dbReference type="Bgee" id="Q63HN8"/>
  <dbReference type="CleanEx" id="HS_KIAA1618"/>
  <dbReference type="ExpressionAtlas" id="Q63HN8">
    <property type="expression patterns" value="baseline and differential"/>
  </dbReference>
  <dbReference type="GO" id="GO:0005737">
    <property type="term" value="C:cytoplasm"/>
    <property type="evidence" value="ECO:0000314"/>
    <property type="project" value="UniProtKB"/>
  </dbReference>
  <dbReference type="GO" id="GO:0016020">
    <property type="term" value="C:membrane"/>
    <property type="evidence" value="ECO:0000314"/>
    <property type="project" value="UniProtKB"/>
  </dbReference>
  <dbReference type="GO" id="GO:0016887">
    <property type="term" value="F:ATPase activity"/>
    <property type="evidence" value="ECO:0000314"/>
    <property type="project" value="UniProtKB"/>
  </dbReference>
  <dbReference type="GO" id="GO:0016874">
    <property type="term" value="F:ligase activity"/>
    <property type="evidence" value="ECO:0000501"/>
    <property type="project" value="UniProtKB-KW"/>
  </dbReference>

当 属性 属性中的“值”以“C:”开头时,我想提取 dbReference 中的“id”值 所以预期的输出是: “去:0005737” “去:0016020”

到目前为止,这是我的脚本:

import urllib2
from lxml import etree

file = urllib2.urlopen('http://www.uniprot.org/uniprot/Q63HN8.xml')
tree = etree.parse(file)
root = tree.getroot()
for node in tree.iter('{http://uniprot.org/uniprot}dbReference'):
   if node.attrib.get('type') == 'GO':
        value = node.attrib.get('value');
        print value
        if value.str.startswith('C:'):
            goterm = node.attrib.get('id')
            print goterm

但离工作还很远。

编辑

另外,如何将不同搜索的值存储到列表中? 预期的: goterm_when_C = ['GO:0005737', 'GO:0016020', 'GO:0005730'] goterm_when_F = ['GO:0016887', 'GO:0016874', 'GO:0004842', 'GO:0008270'] 当我尝试时:

goterm_when_C = []
goterm_when_F = []
            if value.startswith('C:'):
                go_location = node.attrib.get('id')
                for item in go_location:
                    goterm_when_C.append(item)
            if value.startswith('F:'):
                go_function = node.attrib.get('id')
                for item in go_function:
                    goterm_when_F.append(item)
                break

我明白了

>>> goterm_when_C
['G', 'O', ':', '0', '0', '0', '5', '7', '3', '7', 'G', 'O', ':', '0', '0', '1', '6', '0', '2', '0', 'G', 'O', ':', '0', '0', '0', '5', '7', '3', '0']

如有任何帮助,我们将不胜感激

您需要遍历子节点,然后检查其属性。例子-

for node in tree.iter('{http://uniprot.org/uniprot}dbReference'):
   if node.attrib.get('type') == 'GO':
       for child in node:
           value = child.attrib.get('value');
           print value
           if value.startswith('C:'):
               goterm = node.attrib.get('id')
               print goterm
               break