Home > database >  how to export print result to excel using python (for loop)
how to export print result to excel using python (for loop)

Time:01-13

Below is my code and need to generate the result into excel:

a=3
mylist = [{'a': 1, 'b': 2}, {'c': 3, 'd': 4}, {'e': 5, 'f': 6}]
for i,l in zip(mylist,range(a)):
    print('\r')
    print('Result : {0}'.format(l))
    for key,value in i.items():
          print(key,':',value)

CodePudding user response:

I don't know what extension you are talking about when you say Excel. But you can use .csv file to write to a file.

It should go something like this.

with open('out.csv','w') as f:
   for key,value in i.items():
       print(key,',',value)

or you can convert the dictionary into a Pandas Dataframe and export it as .xlsx file using to_excel() function built-in to Pandas. Output

  • Related