I'm trying to compare two groups of account numbers (with Python and xlrd/ xlwt).
The first is a list of my favorite accounts. The seconds is a list of accounts that were recorded when someone from that account called me for help. However, the second list is actually a spreadsheet and has more than just the account numbers, such as a case ID and a category. For instance, account '100' called in about 'Shoes' and was recorded as case #50. (also assume that the spreadsheet has three columns: Account, Category, and Case #).
My objective is to look at the spreadsheet and find any of the times that someone from one of my favorite accounts (from the first list) called in for help. So I basically want to use something like
myFavoriteAccounts = ['100','200','300','400','500']
and then go through the entirety of the spreadsheet, printing any instance where one of my favorite accounts appears, as well as the case ID and the category.
I've been able to find the accounts that appear in both lists:
match = set(myFavoriteAccounts) & set(spreadsheetAccountsColumn)
But I don't know how to iterate through the spreadsheet and catch each time one of those accounts appeared as well as the category and case ID.
I'd like to be able to determine, for instance, that account '100' called in on two separate occasions about 'Shoes' for case #50 and then again for 'Socks' and case #70.
Here's some code as a skeleton.
xls = xlrd.open_workbook(xlsname)
worksheet = xls.sheet_by_name('Accounts') # Use whatever name is on the worksheet tab in excel
max_col = worksheet.ncols - 1 # Cells addressed from 0
max_row = worksheet.nrows - 1 # Cells addressed from 0
account_col = 0 # Assuming it's the first column
for row in xrange(0, max_row):
value = xlrd.cell_value(row, account_col)
if value in favorites:
print "Do something"
print "I can address other cells in this row if I want"
for col in xrange(0, max_col):
new_value = xlrd.cell_value(row, col)
I haven't tested this particular script, but I've used this method in my own programs.