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, 2023 in Others by Kithuzzz
• 38,010 points
4,756 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, 2023 by narikkadan
• 63,420 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,670 points
13,360 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
• 63,420 points
3,628 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
• 63,420 points
2,522 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, 2023 in Others by narikkadan
• 63,420 points
2,104 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 4,055 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
• 63,420 points
859 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
• 63,420 points
3,390 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