Python Dictionary to Multiple CSV files

Question:

So I have a dictionary of 98 elements and I need to write each key:value to a CSV file.

For example:

{‘Group1′:values,’Group2’:values}, etc…

So in this example I would need two (2) csv files:

  1. Group1_output.csv
  2. Group2_output.csv

I am thinking I need a for loop but I am getting a little stuck:

for element in DataFrameDict:
    element.to_csv(f'{element}_output.csv')
Asked By: Grant Culp

||

Answers:

Try this since your data is stored in a dict:

for key, values in dict.items(): 
    f = open("{}_output.csv".format(key),"w")
    f.write(key) 
    for i in values: 
       f.write("," + str(i))
    f.close()
Answered By: nav610

It is not working.
I am getting this error.
‘str’ object has no attribute ‘keys’

Answered By: Rashmi Ranjan
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.