0

这是我正在遍历的 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>
    <alternativeName>
      <fullName>Some other value</fullName>
    </alternativeName><alternativeName>
     ..........

我正在努力达到alternativeName. 我没有任何问题recommended name,所以我尝试使用相同的方法alternativeName。但是,Python解释器输出以下错误消息:

   for child in protein.find("{http://uniprot.org/uniprot}alternativeName"):
TypeError: 'NoneType' object is not iterable

这是Python我用来获取这些元素的代码。同样,该代码适用于recommendedName但不适用于alternativeName. 谢谢你的帮助!

alt_shortnames = []
alt_fullnames = []

protein = e.find("{http://uniprot.org/uniprot}protein")
for child in protein.find("{http://uniprot.org/uniprot}alternativeName"):
    if child.tag == "{http://uniprot.org/uniprot}fullName":
        alt_fullnames.append(child.text)
    if child.tag == "{http://uniprot.org/uniprot}shortName":
        alt_shortnames.append(child.text)

temp_dict["alternativeFullNames"] = alt_fullnames
temp_dict["alternativeShortNames"] = alt_shortnames
4

1 回答 1

1

您正在使用protein.find(); 该.find()方法返回找到的元素或者None如果没有找到。

如果您希望找到一系列元素,请使用.findall(). 该方法总是返回一个可迭代的(可能是空的):

for altName in protein.findall("{http://uniprot.org/uniprot}alternativeName"):
    for child in altName:
        if child.tag == "{http://uniprot.org/uniprot}fullName":
            alt_fullnames.append(child.text)
        if child.tag == "{http://uniprot.org/uniprot}shortName":
            alt_shortnames.append(child.text)
于 2013-05-03T21:44:07.283 回答