Home > Blockchain >  Having troubles to write list in a new row every time during while loop with openpyxl
Having troubles to write list in a new row every time during while loop with openpyxl

Time:12-30

I'm encountering problems inserting the list values into a new row every time the code loops. In a few words, I need the code every, it loops to write the values from lst into a separate row each time. I have done an extensive research to try to understand how this works and found plenty of examples but unfortunately, I couldn't make the code work following those examples.

This is the code:

    max_value = 5
    lst = []

    while True:
        wb = Workbook()
        ws = wb.active
        num = (max_value)
        for n in range(num):
            weight = float(input('Weight: '))
            lst.append(weight)
        ws.append(lst)

        if n 1 == max_value:
            wb.save(filename='path')

I have tried to add ws.insert_rows(idx=2, amount=1) just after the line ws.append(lst) like this:

    ...

    ws.insert_rows(idx=2, amount=1)
    ws.append(lst)

    if n 1 == max_value:
        wb.save(filename='path')

but it doesn't do anything because I suppose it needs something that tells the code to write the next values in that row. I have also tried something like this:

    ...
        
    next_avail_row = len(list(ws.rows))
    ws.append(lst)

    if n 1 == max_value
        wb.save(filename='path')

But here as well I'm not sure how to tell the code after it finds next_avail_row = len(list(ws.rows)) to write in that row.

Thoughts?

EDIT:

At the moment if I enter at the prompt for instance: 1,2,3,4,5

it outputs:

https://i.stack.imgur.com/c13CG.png

if I continue inputting numbers for instance: 7,6,5,4,3

it outputs:

![https://i.stack.imgur.com/RZ2mN.png

and so forth, what I expect to be is:

https://i.stack.imgur.com/3rSIl.png

in a few words, every time the function gets called it writes in the same file but one row below. I hope it is a clear explanation.

CodePudding user response:

There are a couple of issues with your code.

First, in your while loop. Every time that it loops through it is calling wb = Worbook() and eventually wb.save(filename='path'). This is creating a new excel worksheet every time. Assuming that in your wb.save() call that the filename is the same each time, every time you call save on the new workbook it will overwrite the previously made workbook with that same file name.

Next, your list that contains the weight values that you have being input. You aren't clearing the list so each time you add something to it in the loop the list will just keep expanding.

Also the line num = (max_value) doesn't really do anything, and you also need to have some kind of condition to break out of your while loop. Using while True will keep the loop going forever unless you break out of it at some point.

Here is some code that should do this the way that you want it to:

max_value = 5

line_count = 0

wb = Workbook()
ws = wb.active
white True:
   lst = []
   for n in range(max_value):
      weight = float(input('Weight: '))
      lst.append(weight)
   ws.append(lst)

   line_count  = 1
   # an example condition to break out of the loop
   if line_count >= 5:
      break
   
wb.save(filename='path')

Here, your Workbook object is only being created and saved once so it isn't opening a new one each time and overwriting the previous Workbook. The list lst is being emptied each time through the loop so that your lines will only be as long as the value in max_value. I also added in an example way of breaking out of your while True: loop. The way I set it is that once there have been 5 lines that you have added to the workbook, it will break out of the loop. You can create any condition you want to break out of the loop, this is just for example purposes.

  • Related