I know this may be fairly simple for some, buy I am new to python.
I have been looking for a way to write a program specifically for a column on the Excel spreadsheet. An Example (taken from a previous question)
Name Date Age Sex Color
Ray May 25.1 M Gray
Alex Apr 22.3 F Green
Ann Jun 15.7 F Blue
I need all of Alex's information in his row to be displayed when i type Alex. The spreadsheet I am using has thousands of names where everything in the first column is different. I already imported xlrd and the spreadsheet to python (2.7).
My code which i am having problems executing.
from collections import namedtuple Entry = namedtuple('Entry', 'FQDN Primary Secondary') import xlrd file_location = "/Users/abriman26/Desktop/Book1.xlsx" ss_dict = {} spreadsheet = file_location = "/Users/abriman26/Desktop/Book1.xlsx" for row in spreadsheet: entry = Entry(*tuple(row)) ss_dict[entry.Name] = entry
and the error message
Traceback (most recent call last):
File "<pyshell#114>", line 2, in <module>
entry = Entry(tuple(row))
TypeError: __new__() takes exactly 6 arguments (2 given)
I'm assuming that your question was aimed more at how to store information from the spreadsheet in memory so that you could look up data quickly rather than how to use the xlrd library. If this is not the case, please refine your question and include a code example of what you've tried.
If column A is a key, create a dictionary containing tuples or namedtuples.
from collections import namedtuple
Entry = namedtuple('Entry', 'Name Date Age Sex Color')
#open the spreadsheet with xlrd or other spreadsheet library and save into variable named "spreadsheet"
#...
ss_dict = {}
for row in spreadsheet:
entry = Entry(*tuple(row))
ss_dict[entry.Name] = entry
>> ss_dict['Alex']
Entry(Ann, Jun, 15.7, F, Blue)
This will give you fast random access to an entry in the spreadsheet if you know the person's name, especially if you have many entries.