Home > other >  How to write in excel sheet using nested loop
How to write in excel sheet using nested loop

Time:10-06

import xlsxwriter
row = 0
col = 0
a = ['1', '2', '3','4','5']
with xlsxwriter.Workbook('D:\\Master\\Codingfiles\\'   'SSDATA5.xlsx') as workbook:
    worksheet1 = workbook.add_worksheet
    for x in a:
        worksheet1.write(row, col,x)
        row =1

Hi i am a beginner,When i run this code it runs properly and write in values from 'a' in first 5 rows of excel sheet but I want to create a code using nested loop like this

import xlsxwriter
row = 0
col = 0
a = ['1', '2', '3','4','5']
with xlsxwriter.Workbook('D:\\Master\\Codingfiles\\'   'SSDATA5.xlsx') as workbook:
    worksheet1 = workbook.add_worksheet()
for i in a:
    for x in a:
        worksheet1.write(row, col,x)
        row =1

This code does not write above data in excel sheet. Please Help me

CodePudding user response:

In python intendation matters, so try:

import xlsxwriter
row = 0
col = 0
a = ['1', '2', '3','4','5']
with xlsxwriter.Workbook('D:\\Master\\Codingfiles\\'   'SSDATA5.xlsx') as workbook:
    worksheet1 = workbook.add_worksheet()
    for i in a:
        for x in a:
            worksheet1.write(row, col,x)
            row =1

More: https://www.geeksforgeeks.org/indentation-in-python/

CodePudding user response:

Seems like your for loop from the second code is not tabbed properly, and is outside with.

  • Related