Python/LXML-获取;“孙辈”;来自etree
下面是我正在遍历的XML树的一个示例:Python/LXML-获取;“孙辈”;来自etree,python,python-3.x,lxml,elementtree,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</accessio
<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>
..........
e
在此上下文中表示从
到
。当我尝试运行此代码时,Python解释器出现以下错误:
for child in protein.find("recommendedName"):
TypeError: 'NoneType' object is not iterable
所以它告诉我,这里的child
不是一个可移植的对象。我真的不明白,因为蛋白质
肯定是可植入的,所以如果它发现了什么东西,它应该是可植入的。无论如何,我如何使用lxml
API来访问孙子节点recommendedName
和alternativeName
for child in protein.find("recommendedName"):
TypeError: 'NoneType' object is not iterable
错误消息表示protein.find
正在返回None
。因此,未找到推荐名称
元素
由于您正在使用名称空间查找蛋白质
,因此可能需要使用
for child in protein.find("{http://uniprot.org/uniprot}recommendedName")
或者更好
for child in protein.xpath("uniprot:recommendedName",
namespaces = dict(uniprot='http://uniprot.org/uniprot'))
非常感谢,出于某种原因,我认为我只需要根、它的子级和它们的兄弟级的名称空间。我没有意识到我也可以将它用于嵌套元素。
for child in protein.xpath("uniprot:recommendedName",
namespaces = dict(uniprot='http://uniprot.org/uniprot'))