简体   繁体   中英

How to write complete dictionary in existing csv file in loop using python

I want to write the dictionary into an existing .CSV in the new column within the loop.

My sample code :

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

Write code for inserting dictionary in csv file in first loop.

My dictionary data is

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

My desired CSV data should be :

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

You mean like that?

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

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

As output in the file you would get:

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM