【问题标题】:How to write complete dictionary in existing csv file in loop using python如何使用python循环在现有的csv文件中编写完整的字典
【发布时间】:2020-03-26 05:06:45
【问题描述】:

我想将字典写入循环内新列中现有的.CSV

我的示例代码:

for loop:
   keys = []
   values = []
   dictionary = {}`
for loop:
   dictionary = dict(zip(keys,values)

在第一个循环中编写在 csv 文件中插入字典的代码。

我的字典数据是

{ 'string': value, 'string': value }

我想要的 CSV 数据应该是:

Count
{'string':value, 'string':value}

【问题讨论】:

  • 请提供示例数据和预期输出以及您尝试过的内容。目前还不清楚你想做什么。

标签: python pandas csv dictionary


【解决方案1】:

你的意思是这样的?

dict_test = { 'string1': 22, 'string2': 33 }

with open("test.csv", "w+") as file:
    file.write(str(dict_test))

作为文件中的输出,您将获得:

{'string1': 22, 'string2': 33}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-15
    • 1970-01-01
    • 2017-03-01
    • 2019-11-06
    • 1970-01-01
    • 1970-01-01
    • 2019-07-14
    • 2019-01-12
    相关资源
    最近更新 更多