I have a table stored in an Excel file as follows:
Species Garden Hedgerow Parkland Pasture Woodland
Blackbird 47 10 40 2 2
Chaffinch 19 3 5 0 2
Great Tit 50 0 10 7 0
House Sparrow 46 16 8 4 0
Robin 9 3 0 0 2
Song Thrush 4 0 6 0 0
I am using the Python xlrd library to read this data. I have no problem reading it into a list of lists (with each row of the table stored as a list) using the following code:
from xlrd import open_workbook wb = open_workbook("Sample.xls") headers = [] sdata = [] for s in wb.sheets(): print "Sheet:",s.name if s.name.capitalize() == "Data": for row in range(s.nrows): values = [] for col in range(s.ncols): data = s.cell(row,col).value if row == 0: headers.append(data) else: values.append(data) sdata.append(values)
As you can see, headers is a simple list that stores column headers, and sdata contains table data stored as a list of lists. Here is what they are looking at:
headers:
[u'Species', u'Garden', u'Hedgerow', u'Parkland', u'Pasture', u'Woodland']
SData:
[[u'Blackbird', 47.0, 10.0, 40.0, 2.0, 2.0], [u'Chaffinch', 19.0, 3.0, 5.0, 0.0, 2.0], [u'Great Tit', 50.0, 0.0, 10.0, 7.0, 0.0], [u'House Sparrow', 46.0, 16.0, 8.0, 4.0, 0.0], [u'Robin', 9.0, 3.0, 0.0, 0.0, 2.0], [u'Song Thrush', 4.0, 0.0, 6.0, 0.0, 0.0]]
But I want to store this data in a Python dictionary, with each column being the key to a list containing all the values ββfor each column. For example (only part of the data is shown to save space):
dict = { 'Species': ['Blackbird','Chaffinch','Great Tit'], 'Garden': [47,19,50], 'Hedgerow': [10,3,0], 'Parkland': [40,5,10], 'Pasture': [2,0,7], 'Woodland': [2,2,0] }
So my question is: how can I achieve this? I know that I could read data column by column rather than row, as in the code snippet above, but I could not figure out how to store the columns in the dictionary.
Thanks in advance for any help you can provide.