Reputation:
My results set in inside two lists. How can i easily convert them to a csv?
fin= [[ {'name':'value','age':'value','addr':'value'},
{'name':'value','age':'value','addr':'value'},
{'name':'value','age':'value','addr':'value'}
]]
res_csv= pd.DataFrame(fin)
res_csv.to_csv(file.csv)
when i do this, the whole data is getting skewed inside a single line.
Am i doing something incorrect here? Seeking help from the Dev community
Upvotes: 0
Views: 44
Reputation: 9639
This is a nested list, but with only one item - the list you need to load into your dataframe. So you can simply load that list by loading the first (and only) item of that list fin[0]
.
fin= [[ {'name':'value','age':'value','addr':'value'},
{'name':'value','age':'value','addr':'value'},
{'name':'value','age':'value','addr':'value'}
]]
res_csv= pd.DataFrame(fin[0])
res_csv.to_csv('file.csv')
Upvotes: 0
Reputation: 73480
Why use such a heavy tool like pandas for a simple csv:
import csv
with open("file.csv", "w") as f:
w = csv.DictWriter(f, fieldnames=fin[0][0].keys())
w.writerows(fin[0])
Upvotes: 2