1

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).

Please Help!

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)
Matt Ellen
  • 11,268
  • 4
  • 68
  • 90
user3726972
  • 73
  • 1
  • 7

1 Answers1

2

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.

IceArdor
  • 1,961
  • 19
  • 20
  • I appreciate the help! Literally spent this and last week learning Python, and you're absolutely correct, this is what i needed. I ran your code but kept getting the error Traceback (most recent call last): File "", line 2, in entry = Entry(*tuple(row)) TypeError: __new__() takes exactly 6 arguments (2 given) Not sure what to do, i used it for my spreadsheet and got a similar error, still a noob here sorry! – user3726972 Jun 11 '14 at 04:46
  • Learn about the unpack argument list operator (*) here: https://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists and http://stackoverflow.com/questions/2921847/python-once-and-for-all-what-does-the-star-operator-mean-in-python. To solve this problem yourself, try printing out `len(tuple(row))` in the for-loop. Is the length consistent with your definition of the `Entry` namedtuple class for all rows? – IceArdor Jun 11 '14 at 15:58
  • It worked thanks! Unfortunately, now they want me to convert it to a csv, then do the exact same thing – user3726972 Jun 12 '14 at 10:05
  • use the `csv` python module (`import csv`) – IceArdor Jun 12 '14 at 15:53
  • Hey, its been a few days and i still cant completely get it, my csv, has over 1000 rows, 3 columns, i need to read a specific row, but not number based. The only tutorials i see show row[5], i need to input the actual name, ajj56exp.tub.com, and next to it will be 2 names. Should i just post another question with a clean start? – user3726972 Jun 16 '14 at 16:42