【问题标题】:Exporting list of dictionaries to excel将字典列表导出到excel
【发布时间】:2019-12-23 11:53:31
【问题描述】:

我有一个字典列表,正在尝试将其导出到 excel 中。

P = {"@odata.context":"https://remedy.data/network","value"[{"CI_Name":"EB2CMA01","Manufacturer":"CiscoSystems","Product_Categorization_Tier1":"HardwareInfrastructure","Product_Categorization_Tier2":"Network","Product_Categorization_Tier3":"Switch","Company":"4584",},{"CI_Name":"EB2CMA02","Manufacturer":"CiscoSystems","Product_Categorization_Tier1":"HardwareInfrastructure","Product_Categorization_Tier2":"Network","Product_Categorization_Tier3":"Switch","Company":"4584"}]}

我需要将 CI_Name 保存在 .TXT 文件 (here for e:g EB2CMA01 & EB2CMA02) 中,并从该 .txt 文件中获取这些名称并将其与字典相匹配,然后导出该字典的其余 key:value 以使用相应的 VALUE在 KEY 下作为行 & 列。

【问题讨论】:

  • 我会将字典读入 Pandas Dataframe,然后使用 Pandas to_excel。
  • 您的语句中有语法错误。你需要一个 : 在“value”关键字之后。

标签: python excel python-2.7 dictionary


【解决方案1】:

你可以使用 pandas 来安装 pandas > pip install pandas

import pandas as pd

P = {"@odata.context":"https://remedy.data/network","value":[{"CI_Name":"EB2CMA01","Manufacturer":"CiscoSystems","Product_Categorization_Tier1":"HardwareInfrastructure","Product_Categorization_Tier2":"Network","Product_Categorization_Tier3":"Switch","Company":"4584",},{"CI_Name":"EB2CMA02","Manufacturer":"CiscoSystems","Product_Categorization_Tier1":"HardwareInfrastructure","Product_Categorization_Tier2":"Network","Product_Categorization_Tier3":"Switch","Company":"4584"}]}
df=pd.DataFrame.from_dict(P['value'])
df.to_excel('test.xlsx')

【讨论】:

  • 但我必须从列出 EB2CMA01 和 EB2CMA02 的 .txt 中获取输入
  • @Jintu 可以添加你的txt吗?
  • my.txt 文件将有 EB2CMA01 和 EB2CMA02
  • 收到此错误:8 #print response.content 9 P = response.content ---> 10 df=pd.DataFrame.from_dict(P['value']) 11 df.to_excel('test.xlsx') TypeError: string indices must be integers, not str
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-30
  • 1970-01-01
  • 2016-09-10
相关资源
最近更新 更多