Houdini
Houdini

Reputation: 3542

Python/LXML - Children node returning 'NoneType'

Here is a sample of the XML tree I am traversing:

<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>
     ..........

I am trying to get to alternativeName. I do not have any trouble getting to recommended name, so I try the same approach with alternativeName. However, the Python interpretor outputs the following error message:

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

Here is the Python code I am using to get at these elements. Again, the code works fine for recommendedName but NOT for alternativeName. Thanks for any help!

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

Upvotes: 0

Views: 456

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121744

You are using protein.find(); the .find() method returns either the found element or None if nothing is found.

If you expect to find a sequence of elements, use .findall(). That method always returns an iterable (possibly empty):

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)

Upvotes: 1

Related Questions