Python / LXML - 从etree获得“孙子”

时间:2013-05-03 19:14:27

标签: python python-3.x lxml elementtree

以下是我正在遍历的XML树的示例:

<entry dataset="Swiss-Prot" created="1993-07-01+01:00" modified="2013-04-03+01:00" version="144">
  <accession>P31750</accession>
  <accession>Q62274</accession>
  <accession>Q6GSA6</accession>
  <name>AKT1_MOUSE</name>
  <protein>
    <recommendedName>
      <fullName>RAC-alpha serine/threonine-protein kinase</fullName>
      <ecNumber>2.7.11.1</ecNumber>
    </recommendedName>
    <alternativeName>
      <fullName>AKT1 kinase</fullName>
    </alternativeName><alternativeName>
      <fullName>Protein kinase B</fullName>
     ..........

我正在尝试访问recommendedName,这是我用来访问它的当前Python代码:

protein = e.find("{http://uniprot.org/uniprot}protein")
r_names = []
for child in protein.find("recommendedName"):
     for subchild in child.find("fullName"):
          r_names.append(subchild.text)
此上下文中的

e代表<entry></entry>。当我尝试运行此代码时,我从Python解释器得到以下错误:

for child in protein.find("recommendedName"):
  TypeError: 'NoneType' object is not iterable

所以它告诉我child这里不是一个可迭代的对象。我真的不明白,因为protein肯定是可迭代的,所以如果finds它应该是可迭代的。无论如何,我如何使用lxml API来访问孙子节点recommendedNamealternativeName

1 个答案:

答案 0 :(得分:3)

for child in protein.find("recommendedName"):
  TypeError: 'NoneType' object is not iterable

错误消息是protein.find正在返回None。因此没有找到recommendedName个元素。

由于您使用命名空间来查找protein,因此您可能需要使用

for child in protein.find("{http://uniprot.org/uniprot}recommendedName")

或更好,

for child in protein.xpath("uniprot:recommendedName",
                           namespaces = dict(uniprot='http://uniprot.org/uniprot'))