How to export print result to excel using python for loop

0 votes

Below is my code and need to generate the result in 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)
Jan 13 in Others by Kithuzzz
• 28,700 points
80 views

1 answer to this question.

0 votes

 Use the pandas library

import pandas as pd

a = 3
mylist = [{'a': 1, 'b': 2}, {'c': 3, 'd': 4}, {'e': 5, 'f': 6}]

# Create an empty dataframe
df = pd.DataFrame()

for i, l in zip(mylist, range(a)):
    result = 'Result : ' + str(l)
    for key, value in i.items():
        # Append the result and key-value pairs to the dataframe
        df = df.append({'Result': result, 'Key': key, 'Value': value}, ignore_index=True)

# Save the dataframe to an Excel file
df.to_excel('output.xlsx', index=False)

Output :

 Output

answered Jan 13 by narikkadan
• 53,160 points

Related Questions In Others

0 votes
1 answer

How to convert data from txt files to Excel files using python

Hi , there are few steps to ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,630 points
7,547 views
0 votes
1 answer

How to convert pdf file to excel file using python

Just specify your whole output path instead ...READ MORE

answered Nov 4, 2022 in Others by narikkadan
• 53,160 points
1,075 views
0 votes
1 answer

How to convert excel to PDF using Python

Specify your whole output path instead of ...READ MORE

answered Nov 10, 2022 in Others by narikkadan
• 53,160 points
632 views
0 votes
1 answer

How to increment the Range of a For Each loop - Excel VBA

Your formula seems to sum 1 single ...READ MORE

answered Jan 7 in Others by narikkadan
• 53,160 points
79 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 3,323 views
0 votes
1 answer
0 votes
1 answer

How to print an Excel Sheet using VBA with Nitro PDF Creator in Excel 2016

you can use the built-in excel facilities ...READ MORE

answered Sep 24, 2022 in Others by narikkadan
• 53,160 points
234 views
0 votes
1 answer

How to paste an Excel chart into PowerPoint placeholder using Python?

Very near indeed! Copy and Paste are ...READ MORE

answered Oct 7, 2022 in Others by narikkadan
• 53,160 points
604 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP