Sep-19-2018, 12:27 PM
(Sep-18-2018, 08:59 PM)volcano63 Wrote:(Sep-18-2018, 07:48 PM)ankey Wrote:That was pretty clear - using my advice solves this issue. (Still, posting of traceback should be done before you are asked for it - helping others to help yourself.) One thing -
Output:--------------------------------------------------------------------------- ValueError Traceback (most recent call last) C:\python\openpyxl_test.py in <module>() 11 for row in sheet.iter_rows(min_col=1, min_row=1, max_col=2, max_row=sheet.max_row): 12 # occur = int(row[1].value) ---> 13 if int(row[1].value) == 1: 14 pass 15 else: ValueError: invalid literal for int() with base 10: 'occurances'pass
(in most cases) is redundant - if you have to choose a negative test condition, just use the negative test without elseif str(row[1].value) != '1': print(row[0].value)
Thank you for the help. Using string does not work as I'm trying to carry out a condition that is a number comparison so I'm still stuck trying to convert the value to a integer.
Here is the code:
# Import `load_workbook` module from `openpyxl` from openpyxl import load_workbook # Load in the workbook wb = load_workbook('myFile.xlsx') #activate a sheet into array sheet = wb['Sheet1'] # select all populated rows and iterate through for row in sheet.iter_rows(min_col=1, min_row=1, max_col=2, max_row=sheet.max_row): # occur = int(row[1].value) if str(row[1].value) > 1: pass else: print(row[0].value)and here is the error:
Error:---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
C:\python\openpyxl_test.py in <module>()
11 for row in sheet.iter_rows(min_col=1, min_row=1, max_col=2, max_row=sheet.max_row):
12 # occur = int(row[1].value)
---> 13 if str(row[1].value) > 1:
14 pass
15 else:
TypeError: '>' not supported between instances of 'str' and 'int'