Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access a dictionary value for use with the urllib module in python?

Example - I have the following dictionary...

URLDict = {'OTX2':'http://lsdb.hgu.mrc.ac.uk/variants.php?select_db=OTX2&action=view_all',
'RAB3GAP':'http://lsdb.hgu.mrc.ac.uk/variants.php?select_db=RAB3GAP1&action=view_all',
'SOX2':'http://lsdb.hgu.mrc.ac.uk/variants.php?select_db=SOX2&action=view_all',
'STRA6':'http://lsdb.hgu.mrc.ac.uk/variants.php?select_db=STRA6&action=view_all',
'MLYCD':'http://lsdb.hgu.mrc.ac.uk/variants.php?select_db=MLYCD&action=view_all'}

I would like to use urllib to call each url in a for loop, how can this be done?

I have successfully done this with with the urls in a list format like this...

OTX2 = 'http://lsdb.hgu.mrc.ac.uk/variants.php?select_db=OTX2&action=view_all'
RAB3GAP = 'http://lsdb.hgu.mrc.ac.uk/variants.php?select_db=RAB3GAP1&action=view_all'
SOX2 = 'http://lsdb.hgu.mrc.ac.uk/variants.php?select_db=SOX2&action=view_all'
STRA6 = 'http://lsdb.hgu.mrc.ac.uk/variants.php?select_db=STRA6&action=view_all'
MLYCD = 'http://lsdb.hgu.mrc.ac.uk/variants.php?select_db=MLYCD&action=view_all'

URLList = [OTX2,RAB3GAP,SOX2,STRA6,PAX6,MLYCD]

for URL in URLList:                                                        
    sourcepage = urllib.urlopen(URL)                                     
    sourcetext = sourcepage.read() 

but I want to also be able to print the key later when returning data. Using a list format the key would be a variable and thus not able to access it for printing, I would lonly be able to print the value.

Thanks for any help.

Tom

like image 853
Tom Smith Avatar asked Nov 24 '25 02:11

Tom Smith


1 Answers

Have you tried (as a simple example):

for key, value in URLDict.iteritems():
    print key, value
like image 97
Jon Clements Avatar answered Nov 25 '25 15:11

Jon Clements



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!