2

I'm working with creating excel sheets using python. I'm trying to use a nested for loop to fill in some cells on a spreadsheet and it's not going well. What I want it to do is for each row in a given list of rows I want it to enter an even number into the cell. So basically it should look something like this: 2 4 6 8 etc. (One value per cell)

but instead it comes out like: 24 24 24 24

All the cells have the same value.

Aside from the obvious formatting issues (I'm not finished with the formatting part), it prints the last number in the nested loop for every single cell. From my testing it it appears to be fully executing the inner loop for every cell. I'm using XlsWriter if that helps. I'm not sure how to get it to stop.I'm guessing it's pretty obvious but I haven't done any actual "programming" in years so the solution is eluding me. Here's the loop in question:

for items in purple_rows:
     riser_cable.write(items,0,'Company Name',format8)
     riser_cable.write(items,1,None,format8)
     riser_cable.write(items,2,None,format8)
     riser_cable.write(items,3,'Riser',format8)
     for i in range(2,26,2):
         riser_cable.write(items,4,i,format8)
         print(i)

The last 3 lines are ones causing problems. Thanks for the help!

Edit: The sheet should look like this https://i.sstatic.net/RGXbg.jpg but the code presently turns the entire "Port" column to 24.

0

3 Answers 3

2

Your line:

for i in range(2,26,2):
         riser_cable.write(items,4,i,format8)

Writes all the numbers into the same cell. You therefore see only the last number, 24. This is because the item variable is not increased. Try

for i,items in enumerate(purple_rows):
    riser_cable.write(items,4,(i*2) % 25,format8).  

This should increase item, and put a different value in each row.

Sign up to request clarification or add additional context in comments.

1 Comment

That sort of helped. I should have mentioned that purple_rows =[5,7,9,11,13,15,17,19,21,23,25,27,33,35,37,39,41,43,45,47,49,51,53,55,61,63,65,67,69,71,73,75,77,79,81,83,89,91,93,95,97,99,101,103,105,107,109,111] but I only need it to number up to 24 and then repeat.
1

the looping convention in python is exclusive so it loops from 2 to 26, not including 26

otherwise, your loop is fine

riser_cable.write(items,4,i,format8)

it seems you are just updating the 4th column with 2,4,...,24. You would have to increment the column index as well

Try this

 for i in range(1,13):
     riser_cable.write(items,3+i,i*2,format8)
     print(i) 

1 Comment

That has the same issue. I want it to go to the next row. So ideally the results would all be in the same cell but increased by 2. I'd have 2 [blank row] 4 [blank row] 6 [blank row] up to 24. I tried your suggestion and it still just prints out 24 for every cell.
1

I figured out the issue. I ended up changing the code to include a count so it's:

count=0    
for items in purple_rows:
         count+=2
         riser_cable.write(items,4,count,format8)
         riser_cable.write(items,10,count,format8)
         if count==24:
             count=0

Thanks everyone! That fixed the issue

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.