dark horse
dark horse

Reputation: 447

Making for loop dynamic in xlsxwriter module

I have the below loop statement that runs through 'report' object and updates value in an excel sheet as shown:

row = 1
col = 0

for e1, e2, e3 in report:
    worksheet1.write(row, col, e1, number_format)
    worksheet1.write(row, col + 1, e2, number_format)
    worksheet1.write(row, col + 2, e3, number_format)
    row += 1

As seen there are 3 columns that I am trying to update namely e1, e2 and e3. I am trying to see if I can make this for loop dynamic such that it can run through any number of columns without having to define it.

Could anyone advice on this. Thanks

Upvotes: 0

Views: 54

Answers (1)

Frederik Petersen
Frederik Petersen

Reputation: 478

Maybe I misunderstood, but wouldn't you be able to do this?

current_row = 1

for values in report:
    for idx, value in enumerate(values):
        worksheet1.write(current_row, idx, value, number_format)
    current_row += 1

Basically this doesn't specify e1, e2, e3 but writes the tuple to values and then a new for loop iterates over that tuple and is independent of the number of entries.

Upvotes: 2

Related Questions