【问题标题】:Python - array of dictionaries to excel spreadsheet or csv [closed]Python - 用于 Excel 电子表格或 csv 的字典数组 [关闭]
【发布时间】:2019-01-08 07:58:24
【问题描述】:

我有多个这样的数组

 - [{'ID': 001},{'Document': 'Documentation_Name_1'}]
 - [{'ID': 002},{'Document': 'Documentation_Name_2'}]

我想将其导出为 excel 或 CSV 文件 有两个(ID 和 Document)列和多行

任何建议或帮助将不胜感激。提前致谢

【问题讨论】:

  • SO 不是代码编写服务。到目前为止,您尝试过什么?
  • 我尝试过使用 tablib 和 csv 编写器库,但我被困在无法根据字典中的键插入多行的地方
  • @CalvinTey 如果您包含到目前为止尝试过的代码,人们可以更轻松地建议您可以采取哪些措施来解决您的问题。

标签: python excel python-3.x csv


【解决方案1】:

如果您不介意使用 pandas,您可以:

连接你的dicts:

{'ID': '001', 'Document': 'Documentation_Name_1'}

然后列出你的字典:

[{'ID': 001,'Document': 'Documentation_Name_1'}, {'ID': 002, 'Document': 'Documentation_Name_2'}]

将您的 dicts 列表导出到 pandas 数据框:

final_df = pd.DataFrame(YOUR_LIST_OF_DICTS, columns=["ID", "Document"])

然后将数据框导出为 csv 文件:

final_df.to_csv("filename.csv")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-05
    • 2021-09-03
    • 2011-12-23
    • 1970-01-01
    • 2011-06-12
    • 2020-01-28
    相关资源
    最近更新 更多