Python Forum
Assigning iter_row value to variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assigning iter_row value to variable
#1
I'm trying to load and read through an excel file and based on the value of a cell write the row to another file.

Problem I'm having now is that row[1].value is a string and I need to convert it to a number so that I can apply a great than > condition

The code I have so far which works based on the == is this:

# 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):
	if row[1].value == 1:
		pass
	else: 
		print(row[0].value)
If I change the condition in within the loop to a "greater than" like this:

# 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):
	if row[1].value == 1:
		pass
	else: 
		print(row[0].value)
Then I get the error:
TypeError: '>' not supported between instances of 'str' and 'int'

Any ideas?
Reply
#2
if int(row[1].value) == 1:
Reply
#3
(Sep-18-2018, 03:48 PM)Larz60+ Wrote:
if int(row[1].value) == 1:

Thanks for the input Larz60+ but when I try the suggested change I get the following error:

ValueError: invalid literal for int() with base 10: 'occurances'
Reply
#4
You have to post the complete error message to get some help. And post the code again if you have changed it, as there is no "occurances" in any of the code you posted.
Reply
#5
(Sep-18-2018, 04:48 PM)woooee Wrote: You have to post the complete error message to get some help. And post the code again if you have changed it, as there is no "occurances" in any of the code you posted.

occurances - I would hazard a guess - is the cell content
if int(row[1].value) == 1:
This will be safe
if str(row[1].value) == '1':
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#6
Thanks for the help. Here's the full 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 int(row[1].value) == 1:
		pass
	else: 
		print(row[0].value)
And error:
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'
Reply
#7
(Sep-18-2018, 07:48 PM)ankey Wrote:
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'

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 - pass (in most cases) is redundant - if you have to choose a negative test condition, just use the negative test without else
if str(row[1].value) != '1':
    print(row[0].value)
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#8
(Sep-18-2018, 08:59 PM)volcano63 Wrote:
(Sep-18-2018, 07:48 PM)ankey Wrote:
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'
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 - pass (in most cases) is redundant - if you have to choose a negative test condition, just use the negative test without else
if 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'
Reply
#9
Although I feel really dumb I just wanted to share the problem in case anyone finds the thread in the future....

The problem was that I left the column headers in the sheet one of which was 'occurrences'. So when I used the str it fell over on the first row.

Apologies for dumb mistake but thanks to those who helped me. I consider myself schooled and I'm sure I'll not get caught out by this again.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Assigning a new value to variable uriel 1 1,590 Dec-04-2021, 02:59 PM
Last Post: Underscore
  assigning a variable :( gr3yali3n 0 1,314 Sep-22-2020, 09:02 PM
Last Post: gr3yali3n
  Assigning variables Godserena 4 2,166 Apr-26-2020, 06:59 AM
Last Post: buran
  Assigning a Variable Help MC2020 5 2,936 Jan-06-2020, 10:54 PM
Last Post: MC2020
  Assigning an item from a list xlev 1 1,452 Sep-27-2019, 04:42 PM
Last Post: Larz60+
  assigning index 3Pinter 6 3,003 Jan-18-2019, 10:07 PM
Last Post: nilamo
  Not adding and assigning? 00712411 7 4,157 Oct-10-2018, 07:11 PM
Last Post: volcano63
  Assigning a new variable in a IF loop pythoneer 5 3,783 Mar-02-2018, 05:21 AM
Last Post: pythoneer
  Assigning to string slice michaeljhuman 1 2,755 Feb-08-2018, 12:57 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020