Help with Excel, Python and XLRD?

Couple of pointers: i'd suggest you not print your function with no return value, instead just call it, or return something to print def readRows(): for rownum in range(sh1. Nrows): rows = sh1. Row_values(rownum) userNm = rows4 Password = rows5 supID = rows6 print userNm, Password, supID readRows() or looking at the docs you can take a slice from the row_values: row_values(rowx, start_colx=0, end_colx=None) # Returns a slice of the values of the cells in the given row because you just want rows with index 4 - 6: def readRows(): # using list comprehension return sh1.

Row_values(idx, 4, 6) for idx in range(sh1. Nrows) print readRows() using the second method you get a list return value from your function, you can use this function to set a variable with all of your data you read from the excel file. The list is actually a list of lists containing your row values L1 = readRows() for row in L1: print row0, row1, row2 After you have your data, you are able to manipulate it by iterating through the list, much like for the print example above def login(name, password, id): # do stuff with name password and id passed into method ... for row in L1: login(row) you may also want to look into different data structures for storing your data.

If you need to find a user by name using a dictionary is probably your best bet: def readRows(): rows = sh1. Row_values(idx, 4, 6) for idx in range(sh1. Nrows) # using list comprehension return dict( row4, (row5, row6) for row in rows ) D1 = readRows() print D'Bob' ('sdfadfadf',23) import pprint pprint.

Pprint(D1) {'Bob': ('sdafdfadf',23), 'Cat': ('asdfa',24), 'Dog': ('fadfasdf',24)} one thing to note is that dictionary values returned arbitrarily ordered in python.

Couple of pointers: i'd suggest you not print your function with no return value, instead just call it, or return something to print. Def readRows(): for rownum in range(sh1. Nrows): rows = sh1.

Row_values(rownum) userNm = rows4 Password = rows5 supID = rows6 print userNm, Password, supID readRows() or looking at the docs you can take a slice from the row_values: row_values(rowx, start_colx=0, end_colx=None) # Returns a slice of the values of the cells in the given row. Because you just want rows with index 4 - 6: def readRows(): # using list comprehension return sh1. Row_values(idx, 4, 6) for idx in range(sh1.

Nrows) print readRows() using the second method you get a list return value from your function, you can use this function to set a variable with all of your data you read from the excel file. The list is actually a list of lists containing your row values. L1 = readRows() for row in L1: print row0, row1, row2 After you have your data, you are able to manipulate it by iterating through the list, much like for the print example above.

Def login(name, password, id): # do stuff with name password and id passed into method ... for row in L1: login(row) you may also want to look into different data structures for storing your data. If you need to find a user by name using a dictionary is probably your best bet: def readRows(): rows = sh1. Row_values(idx, 4, 6) for idx in range(sh1.

Nrows) # using list comprehension return dict( row4, (row5, row6) for row in rows ) D1 = readRows() print D'Bob' ('sdfadfadf',23) import pprint pprint. Pprint(D1) {'Bob': ('sdafdfadf',23), 'Cat': ('asdfa',24), 'Dog': ('fadfasdf',24)} one thing to note is that dictionary values returned arbitrarily ordered in python.

Thank you for the awesome reply. I was curious on the usage of dictionaries for this particular problem. One thing: when I use the second example I get a NameError that rownum is not defined - do I still need the for loop?

– over Apr 14 at 18:01 @over oops let me edit that... sorry – DTing Apr 14 at 18:08.

Seems to be good. With one remark: you should replace "rows" by "cells" because you actually read values from cells in every single row.

I'm not sure if you are intent on using xlrd, but you may want to check out PyWorkbooks (note, I am the writter of PyWorkbooks :D) from PyWorkbooks. ExWorkbook import ExWorkbook B = ExWorkbook() B. Change_sheet(0) # Note: it might be B:1000, 3:6.

I can't remember if xlrd uses pythonic addressing (0 is first row) data = B:1000,4:7 # gets a generator, the '1000' is arbitrarily large. Def readRows() while True: try: userNm, Password, supID = data.next() # you could also do data0 print userNm, Password, supID if usrNm == None: break # when there is no more data it stops except IndexError: print 'list too long' readRows() You will find that this is significantly faster (and easier I hope) than anything you would have done. Your method will get an entire row, which could be a thousand elements long.

I have written this to retrieve data as fast as possible (and included support for such things as numpy). In your case, speed probably isn't as important. But in the future, it might be :D Check it out.

Documentation is available with the program for newbie users. http://sourceforge.net/projects/pyworkbooks.

Berg I'm definitely going to check this out. Been looking for something to use inplace of xlrd. Thanks.

– DTing Apr 15 at 0:28.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions