[英]Convert dictionary that is present inside two lists into a csv
我的结果设置在两个列表中。 我怎样才能轻松地将它们转换为 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
为什么要使用 pandas 之类的重型工具来制作简单的 csv:
import csv
with open("file.csv", "w") as f:
w = csv.DictWriter(f, fieldnames=fin[0][0].keys())
w.writerows(fin[0])
这是一个嵌套列表,但只有一项 - 您需要加载到 dataframe 的列表。 因此,您可以通过加载该列表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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.