How to iterate esearch and efetch
1
0
Entering edit mode
7.2 years ago
Lilizine ▴ 10

Hi all,

I am new with Biopython :)

def fetchByQuery(query,days):
Entrez.email = "email" # must give NCBI an email address
searchHandle=Entrez.esearch(db="pubmed", reldate =single_date, term=query, usehistory="y")
searchResults=Entrez.read(searchHandle)
searchHandle.close()
webEnv=searchResults["WebEnv"]
queryKey=searchResults["QueryKey"]
batchSize=10
try:
    fetchHandle = Entrez.efetch(db="pubmed", retmax=10, retmode="xml", webenv=webEnv, query_key=queryKey)
    data=fetchHandle.read()
    fetchHandle.close()
    return data
except:
    return None

I have a list of years year = [2000, 2001, 2002 etc] A list of terms terms = [cancer, diabete, etc] I want to extract pubmed for every year since 2000 ( so I need to itrate ! then for every year, I iterate every term and print the result in XML

Can someone help me with iterations ?

software error • 2.5k views
ADD COMMENT
1
Entering edit mode

Thank you ! Now I want to write the output for each tuple ( year, term) in an xml format This is what I have done:

enteryears = range(2017, 2016, -1)
termList=["unmet ","needs""]  

 for year in years:
 for query in termList:
    xml_data=fetchByQuery(query,years)
    if xml_data==None: 
        print 80*"*"+"\n"
        print "This search returned no Pubmed Data"

    else:
        with open(query+'.txt',"w") as temp_file:
                temp_file.write(xml_data(query,years))
                temp_file.close()

The issues: 1- with open(query+'.txt',"w") as temp_file: I want to specify the year and term list in each title of the xml file. 2- The way I am writing the code it returns files with the query names without years and with the same content !

Thanks in advance

ADD REPLY
0
Entering edit mode

You wrote this as a new question. Why add this as an answer and then accept it? I'm moving it to a comment.

ADD REPLY
0
Entering edit mode

Sorry I am new in the forum :)

ADD REPLY
2
Entering edit mode
7.2 years ago
shoujun.gu ▴ 380

like this?

years=[Year_Range]
terms=[Terms]
for year in years:
    for term in terms:
        Your_Fetch_Function(year, term)
ADD COMMENT

Login before adding your answer.

Traffic: 1663 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6