Home > Back-end >  the column that i have added in excel have only one value for all rows
the column that i have added in excel have only one value for all rows

Time:04-20

i'm adding a column in an excel sheet and the values don't seem right

ws["J1"] = "Stars"
ws["J1"].font = Font(bold=True, name='Arial', size=10)
stars =[2,2,4,5,3,5,4,2,3,4,5,2,3,5,4,5,1,4,2,3,2,2,4,5,3,5,3,5,4,5,2,3,5,4,5,4,2,3,3,5,4,5,2,3,5,4,5,4,2,3]
for i in range(2,67):
    for j in range(len(stars)):
        ws.cell(row=i, column=10).value = stars[j]

but then the column has only the nomber 3

CodePudding user response:

Wherever possible, avoid creating your own counters.

for idx, star in enumerate(stars, start=2):
   ws.cell(column=10, row=idx, value=star)

CodePudding user response:

this cause your are rewriting the same cell and 3 is the last number in your array . Try like this:

ws["J1"] = "Stars"
ws["J1"].font = Font(bold=True, name='Arial', size=10)
stars =[2,2,4,5,3,5,4,2,3,4,5,2,3,5,4,5,1,4,2,3,2,2,4,5,3,5,3,5,4,5,2,3,5,4,5,4,2,3,3,5,4,5,2,3,5,4,5,4,2,3]
for i in range(2,67):
    for j in range(len(stars)):
        ws.cell(row=i, column=10).value = stars[j]
    next j
next i
  • Related