-2

I have a "for" loop where I iterate each time to get new values stored in same variable, now i want to store the value stored in the list variable in each rows in excel.

what i tried which doesn't works,

final_result = []
for i in range(0, 3):
    result_list = ['csm_biscuit_main_dev-7188-userdevsigned', 'Logd', 'G0B0LF036224003G', 'Biscuit', 'completed', '1:47:54', 'completed']
    
    . #some functions, which will keep changing the values in "result_list"
    .
    . 

    final_result = final_result.append(result_list)
    wb = Workbook()
    sh1 = wb.active
    sh1.append(result_list)
    wb.save("Report.xlsx")
    print(final_result)

what I am getting is, for each iterations (0, 1, 2) I am getting last iteration's value stored in Report.xlsx.

is there any suggestions to get all iteration's values one by one in rows of excel sheet?

iltech
  • 183
  • 1
  • 2
  • 11
  • 1
    Does this answer your question? [why \[\].append(1) is None](https://stackoverflow.com/questions/18610627/why-append1-is-none) – wovano Oct 26 '22 at 11:17
  • PS: To make this a [mre], remove all the Excel/Workbook stuff. It's not relevant for your question, since the problem can easily be reproduced by only printing `final_result`. – wovano Oct 26 '22 at 11:18
  • I think you didn't understand the problem here.. – iltech Oct 26 '22 at 14:53
  • Then please clarify the problem. Update your question with expected and actual output. Since this isn't a [mre], I can't test it and so have to guess what's the problem. I see that you do `final_result = final_result.append(result_list)`, which doesn't make any sense, as explained in the duplicate target. If this isn't the problem that your question is about, then please remove that from the code. – wovano Oct 26 '22 at 15:39
  • if you write in loop then maybe you need to open file in `append mode`. Or maybe you should create `Workbook()` before loop – furas Oct 26 '22 at 15:52
  • 1
    Maybe first use `print()` (and `print(type(...))`, `print(len(...))`, etc.) to see which part of code is executed and what you really have in variables. It is called `"print debuging"` and it helps to see what code is really doing. – furas Oct 26 '22 at 15:53

1 Answers1

1

You have to create Workbook() before loop. That's all.

Using Workbook() inside loop you create new workbook in every iteration and this removes previous content - and finally you get valur only for last iteration.


Minimal working code:

from openpyxl import Workbook

# --- before loop ---

final_result = []

wb = Workbook()
sh1 = wb.active

# --- loop ---

for i in range(0, 3):
    result_list = ['csm_biscuit_main_dev-7188-userdevsigned', 'Logd', 'G0B0LF036224003G', 'Biscuit', 'completed', '1:47:54', 'completed']
    
    # some functions, which will keep changing the values in "result_list"

    sh1.append(result_list)
    #wb.save("Report.xlsx")

    final_result.append(result_list)
    print(final_result)

# --- after loop ---

wb.save("Report.xlsx")
furas
  • 134,197
  • 12
  • 106
  • 148