简体   繁体   中英

Python Dictionary to Multiple CSV files

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')

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()

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