Home > OS >  How to export an Excel file from a dictionary?
How to export an Excel file from a dictionary?

Time:02-22

I want to make an excel file from a dictionary that I have. It simply is an dictionary with information about images, like the size, how many paragraphs, how many words, etc.

Let's say the dictionary is:

{'Screenshot_1.jpg': {'SIZE': 214649, 
                      'HEIGHT': 664, 
                      'WIDTH': 1351, 
                      'PARAGRAPHS': 3, 
                      'WORDS': 427, 
                      'Paragraph1': {'Words': 28}, 
                      'Paragraph2': {'Words': 0}, 
                      'Paragraph3': {'Words': 0}},
 'Screenshot_2.jpg': {'SIZE': 168279,  
                      'HEIGHT': 664, 
                      'WIDTH': 1348, 
                      'PARAGRAPHS': 2, 
                      'WORDS': 1839,  
                      'Paragraph1': {'Words': 57}, 
                      'Paragraph2': {'Words': 9}}}

The code that I used to extract this dictionary to a csv/excel file it is:

import pandas as pd
import csv
dict1 = {'Screenshot_1.jpg': {'SIZE': 214649, 'HEIGHT': 664, 'WIDTH': 1351, 'PARAGRAPHS': 3, 'WORDS': 427, 'Paragraph1': {'Words': 28}, 'Paragraph2': {'Words': 0}, 'Paragraph3': {'Words': 0}}, 'Screenshot_2.jpg': {'SIZE': 168279, 'HEIGHT': 664, 'WIDTH': 1348, 'PARAGRAPHS': 2, 'WORDS': 1839, 'Paragraph1': {'Words': 57}, 'Paragraph2': {'Words': 9}}}
df = pd.DataFrame(data=dict1, index=[0])
df = (df.T)
print(df)
with open('output.csv', 'w') as output:
    writer = csv.writer(output)
    for key, value in dict1.items():
        writer.writerow([key, value])

And it works well but it gives me this enter image description here

CodePudding user response:

Given that you want those columns you specified, you could do:

df = pd.DataFrame(data=d).T
df = df.loc[:,"SIZE":"PARAGRAPHS"]
df.to_csv("output.csv")

If you want it the excel you can replace the last line with df.to_excel('output.xlsx')

CodePudding user response:

try this if you want to drop columns just add key name in drop list

import pandas as pd

data = {'Screenshot_1.jpg': {'SIZE': 214649, 'HEIGHT': 664, 'WIDTH': 1351, 'PARAGRAPHS': 3, 'WORDS': 427, 'Paragraph1': {'Words': 28}, 'Paragraph2': {'Words': 0}, 'Paragraph3': {'Words': 0}}, 'Screenshot_2.jpg': {'SIZE': 168279, 'HEIGHT': 664, 'WIDTH': 1348, 'PARAGRAPHS': 2, 'WORDS': 1839, 'Paragraph1': {'Words': 57}, 'Paragraph2': {'Words': 9}}}


dropList = ['Paragraph1','Paragraph2']
 for k in data.keys():
     for j in dropList:
         data[k].pop(j)
pdDict = pd.DataFrame(data)
pdDict = pdDict.T
pdDict.to_csv("file.csv")

for excel use pdDict.to_excel("file.xlsx")

  • Related