0

Following the response in this thread. I prepared the following code.

from xlrd import open_workbook

def main(): 
  book = open_workbook('/dir/file.xlsx', index_col=0) 
  sheet = 'Sheet1' 
  collection_year_col = 'Unnamed: 2' 
  test_year = 2011 
  for row in range(sheet.nrows=7): 
      if sheet.cell(row,collection_year_col).value == test_year: 
         runCode() 

def runCode():
    print("worked!")

It runs and gives no error, but never outputs the print().

Why is that and how can I fix it? My value of interest is "2011" and is found in cell C7 of my Excel file.

I suspect the issue is my (bad?) syntax to refer to row 7.

StatsScared
  • 517
  • 6
  • 20
  • 1
    You only defined some stuff, it doesn't look like you're actually running the script though. Cut out the def main(): and def runCode(): lines and associated indentation then see what it does. Replace the runCode() with your print line in the if statement, too. – Dylan Smith Aug 22 '18 at 14:58
  • Have you tried to cast year to string value? – Rafał Aug 22 '18 at 14:58
  • Thanks @DylanSmith. I did what you said and as expected get an error for the line where I define the row. `----> for row in range(sheet.nrows=7):` `AttributeError: 'str' object has no attribute 'nrows'`. I'm convinced I'm not using the proper syntax to call row 7 from my file. I haven't found a proper answer to do it properly. – StatsScared Aug 22 '18 at 15:28
  • @StatsScared I'm sorry I can't help further, I'm not particularly familiar with this method of reading excel. I'm more used to using Pandas. I hope you're able to get it worked out. – Dylan Smith Aug 22 '18 at 15:37
  • @DylanSmith. Thanks in any case. And you are welcome to offer help using Pandas or any other library! (PS. see my other related and initial post [here](https://stackoverflow.com/questions/51938430/python-pandas-loop-to-append-excel-files-only-if-each-excel-file-contains-certa/51951145?noredirect=1#comment90890862_51951145) – StatsScared Aug 22 '18 at 21:44

0 Answers0