Hi everyone,

My code is an attempt to teach myself Python. I am looking to create a series of loops using the range function to read data from excel. My problem doesn't lie with reading the data from excel, I know the functions for this, my problem is setting up the third loop to read a desired set of data off the worksheets.

The problem I have lies with the if loop for incrementing j. All my attempts have ended with the program not reading the data, reading only the first cell for all the new columns, or reading only the last cell value.

I am looking for help to make the if loop increment to cell number 6.

for r in range(11,12):
    for i in range(0,6):
        if (j<7):
            if(ws.Cells(c,8).Value == Iteration):
...
                print'Value of r=',r
                print'Value of r=',i
                print'Value of r=',j
...
            ++j
        if (a == 6):
            break

If I havn't explained myself well enough I can explain more if needed.

Thanks for reading this

Recommended Answers

All 3 Replies

Well your code confuses me. The first instance of j is in a conditional statement comparing it to the number 7. I don't see it even initialized before that. And the same goes for a. Oh, and Iteration - what's the value of that?

Maybe post code explaining what those variables are and what they're for, or at least all 3 for loops that you're using as you said you had trouble with the third, but I only see two posted above.

EDIT:
I just spotted your ++j line. That doesn't do anything as far as I know. You'll need to change it to j += 1 . See below:

>>> j = 0
>>> ++j
0
>>> # j is still zero...
>>> j += 1
>>> j
1
>>> # but += 1 worked

you code confuse me too.

Note this:

>>> range(11,12)
[11]
>>> for i in range(11,12):
	print i

	
11

You're only executing your first loop for when r = 11, ie 1 time only.

Give us a little more code and some context. But yeah, no need for a loop if you only want to do it once. :)

Is this what you want?

>>> for r in range(11,13):
	for i in range(0,7):
		for j in range(0,7):
			print r,i,j

			
11 0 0
11 0 1
11 0 2
11 0 3
11 0 4
11 0 5
11 0 6
11 1 0
11 1 1
11 1 2
11 1 3
11 1 4
11 1 5
11 1 6
11 2 0
11 2 1
11 2 2
11 2 3
11 2 4
11 2 5
11 2 6
11 3 0
11 3 1
11 3 2
11 3 3
11 3 4
11 3 5
11 3 6
11 4 0
11 4 1
11 4 2
11 4 3
11 4 4
11 4 5
11 4 6
11 5 0
11 5 1
11 5 2
11 5 3
11 5 4
11 5 5
11 5 6
11 6 0
11 6 1
11 6 2
11 6 3
11 6 4
11 6 5
11 6 6
12 0 0
12 0 1
12 0 2
12 0 3
12 0 4
12 0 5
12 0 6
12 1 0
12 1 1
12 1 2
12 1 3
12 1 4
12 1 5
12 1 6
12 2 0
12 2 1
12 2 2
12 2 3
12 2 4
12 2 5
12 2 6
12 3 0
12 3 1
12 3 2
12 3 3
12 3 4
12 3 5
12 3 6
12 4 0
12 4 1
12 4 2
12 4 3
12 4 4
12 4 5
12 4 6
12 5 0
12 5 1
12 5 2
12 5 3
12 5 4
12 5 5
12 5 6
12 6 0
12 6 1
12 6 2
12 6 3
12 6 4
12 6 5
12 6 6

You may have better luck if you call your variables something meaningful. Like, cell_number, row_number, etc

you code confuse me too.

Note this:

>>> range(11,12)
[11]
>>> for i in range(11,12):
	print i

	
11

You're only executing your first loop for when r = 11, ie 1 time only.

Give us a little more code and some context. But yeah, no need for a loop if you only want to do it once. :)

Is this what you want?

>>> for r in range(11,13):
	for i in range(0,7):
		for j in range(0,7):
			print r,i,j

			
11 0 0
11 0 1
11 0 2
11 0 3

You may have better luck if you call your variables something meaningful. Like, cell_number, row_number, etc

Sorry, I forgot to add more detail, I thought it would be better to put in the part I was having trouble with.

The variables that are in the code above are all declared in my program the issue I was having was something like what zachabesh posted. The results you have above were what I was getting out, but what I wanted was:

11 0 1
11 1 2
11 2 3
.... etc

Sorry for posting the confusing code. I eventually developed an algorithm for this.

Thanks for your help guys. I'm a fan of Daniweb!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.