How to access Tupla elements?

Asked

Viewed 210 times

0

How to access the elements of this tuple:

StringElement('24929426', attributes={'IdType': 'pubmed'})

I want to extract the attributes:

'24929426'
'IdType': 'pubmed'

The excerpt of the code that generates this output:

>>> from Bio import Entrez
>>> Entrez.email = "[email protected]"     # Always tell NCBI who you are
>>> handle = Entrez.esearch(db="pubmed", term="biopython")
>>> record = Entrez.read(handle)
>>> record["IdList"]
['24929426', '24497503', '24267035', '24194598', '23842806', '23157543', '22909249', '22399473', '21666252', '21210977', '20015970', '19811691', '19773334', '19304878', '18606172', '21585724', '16403221', '16377612', '14871861', '14630660']
>>> idlist = record["IdList"][0]
>>> handle = Entrez.efetch(db="pubmed", id=idlist, rettype="medline", retmode="xml")
>>> records = Entrez.read(handle)
>>> records[0]['PubmedData']
{'History': [DictElement({'Year': '2014', 'Month': '1', 'Day': '22'}, attributes={'PubStatus': 'received'}), DictElement({'Year': '2014', 'Month': '6', 'Day': '10'}, attributes={'PubStatus': 'accepted'}), DictElement({'Hour': '6', 'Year': '2014', 'Month': '6', 'Day': '16', 'Minute': '0'}, attributes={'PubStatus': 'entrez'}), DictElement({'Hour': '6', 'Year': '2014', 'Month': '6', 'Day': '16', 'Minute': '0'}, attributes={'PubStatus': 'pubmed'}), DictElement({'Hour': '6', 'Year': '2015', 'Month': '4', 'Day': '22', 'Minute': '0'}, attributes={'PubStatus': 'medline'})], 'ArticleIdList': [StringElement('24929426', attributes={'IdType': 'pubmed'}), StringElement('1756-0500-7-365', attributes={'IdType': 'pii'}), StringElement('10.1186/1756-0500-7-365', attributes={'IdType': 'doi'}), StringElement('PMC4094456', attributes={'IdType': 'pmc'})], 'PublicationStatus': 'epublish'}
>>> records[0]['PubmedData']['ArticleIdList']
[StringElement('24929426', attributes={'IdType': 'pubmed'}), StringElement('1756-0500-7-365', attributes={'IdType': 'pii'}), StringElement('10.1186/1756-0500-7-365', attributes={'IdType': 'doi'}), StringElement('PMC4094456', attributes={'IdType': 'pmc'})]
  • How is this being generated StringElement('24929426', attributes={'IdType': 'pubmed'})?

  • is the return of a key of a dictionary

  • records[0]['PubmedData']['ArticleIdList'][0] to generate the above output StringElement('24929426', attributes={'IdType': 'pubmed'})

  • It has many types of data like this StringElement('Computational Biology', attributes={'MajorTopicYN': 'N', 'UI': 'D019295'}) is of type <class 'Bio.Entrez.Parser.Stringelement'>

  • Try something like: dict(records[0]['PubmedData']['ArticleIdList'][0]), or str(records[0]['PubmedData']['ArticleIdList'][0]), or list(records[0]['PubmedData']['ArticleIdList'][0]) see what happens. And if you do records[0]['PubmedData']['ArticleIdList'].attributes?

  • It didn’t work. It just took the first attribute

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.