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
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])
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')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.