[Biopython] Problem to retreive compound names using CID from PubChem

Brad Chapman chapmanb at 50mail.com
Mon May 23 06:42:56 EDT 2011


Sainitin;

> Code
> h = Entrez.esummary(db = "pccompound",id = "449489")
> r = Entrez.read(h)
> r[0]["SourceName"]
> 
> Error
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> KeyError: 'SourceName'
> 
> Can anybody help me to solve this

The 'r' object you've parsed from Entrez contains a list of
dictionaries. The information that is in each dictionary will be
dependent on the database you are retrieving from. In this case
there is no SourceName information, so python returns a KeyError to
indicate this.

You can examine the items in the dictionary with:

for key, val in r[0].iteritems():
    print key, val

[...]
InChI InChI=1S/C9H12IN2O8P/c10-4-2-12(9(15)11-8(4)14)7-1-5(13)6(20-7)3-19-21(16,17)18/h2,5-7,13H,1,3H2,(H,11,14,15)(H2,16,17,18)/t5-,6+,7+/m0/s1
TautomerCount 3
SourceIDList []
BondChiralCount 0
MeSHTermList ["5-iodo-2'-deoxyuridine 5'-monophosphate", '5-iodo-dUMP', 'IdUMP', 'iododeoxyuridylate', 'iododeoxyuridylate, 125I-labeled']
[...]

There are also a number of good online resources for learning Python
which will help give experience in debugging these kind of errors:

http://learnpythonthehardway.org/index
http://diveintopython.org/

Hope this helps,
Brad


More information about the Biopython mailing list