Reputation: 3
I tried to search for my problem, but I couldn't find anything closley related, so I decided to put my question in here. I'm pretty new to Python and still learning this languange, so forgive me if I didn't catch obvious things.
Currently I'm trying to extract data via beautifulsoup from some letters that were transformed into .xml-files. Those extracted data - I put them into a nested dictionary. What I want to accomplish is a csv-file which contains all those data.
I've got a nested dictionary like this:
dict = [
{"id":"0", "foundPersons" : ["Smith, Bernhard","Jackson, Andrew","Dalton, Henry"], "sendfrom" : "Calahan, Agnes"},
{"id":"1", "foundPersons" : ["Cutter, John","Ryan, Jack"], "sendfrom" : "Enrico, Giovanni"}
{"id":"2", "foundPersons" : "Grady, Horace", "sendfrom" : "Calahan, Agnes"}
]
I want to have ID, foundPersons and sendfrom als the header of each column. Then below the ID header I want to have every ID in each cell, every foundPerson of one ID into one cell (that means that those 3 names of ID0 are in one cell) and so on.
I tried that by using the csv module but I couldn't figure out how to accomplish that. Can somebody maybe give me a hint? Or is there any other module / library which can help me out here?
Upvotes: 0
Views: 360
Reputation: 168
You want to use the pandas library here.
import pandas as pd
data = pd.DataFrame.from_dict(dict)
data.to_csv('output.csv')
print(data)
And your data is now organized to columns and rows.
Upvotes: 1