Search code examples
pythoncsvexport-to-csv

How to write the values of a dictionary into a CSV file


This is my Python version:

import sys

print(sys.version)

>> 3.6.9 (default, Nov  7 2019, 10:44:02) 
>> [GCC 8.3.0]

And I'm running on a Google Colaboratory notebook.

I'm newbie with Python and I haven't found any useful example to show me how to write the following dictionary to a CSV.

print(my_dict)

>> {'loss': [0.411, 0.115, 0.147, 0.078, 0.032], 'accuracy': [0.996, 0.997, 0.997, 0.997, 0.997], 'val_loss':  [0.147, 0.100, 0.187, 0.065, 0.052], 'val_accuracy': [0.99, 0.99, 0.99, 0.996, 0.996]}

And:

print(type(my_dict))

>> <class 'dict'>

And I want to get this CSV file:

loss,accuracy,val_loss,val_accuracy
0.411,0.996,0.147,0.99
0.115,0.997,0.100,0.99
0.147,0.997,0.187,0.99
0.078,0.997,0.065,0.996
0.032,0.997,0.052,0.996

The values for each key as columns.

I'm trying with this code (I'm using stdout to see what it is happening):

import sys
import csv

w = csv.DictWriter(sys.stdout, my_dict.keys())
w.writeheader()
w.writerows(my_dict.values())

But I get the error:

AttributeError: 'list' object has no attribute 'keys'

I have changed the last line with this:

w.writerows(my_dict)

And I get the same error:

AttributeError: 'str' object has no attribute 'keys'

And with this:

w.writerows(results.history.items())

With the error:

AttributeError: 'tuple' object has no attribute 'keys'

And also, I have tried:

for key, value in my_dict.items():
  w.writerow([key,value])

With the error:

AttributeError: 'list' object has no attribute 'keys'

Another test:

for data in my_dict:
  w.writerow(data)

With the error:

AttributeError: 'str' object has no attribute 'keys'

How can I do it?


Solution

  • import csv
    
    my_dict = {'loss': [0.411, 0.115, 0.147, 0.078, 0.032], 'accuracy': [0.996, 0.997, 0.997, 0.997, 0.997], 'val_loss':  [0.147, 0.100, 0.187, 0.065, 0.052], 'val_accuracy': [0.99, 0.99, 0.99, 0.996, 0.996]}
    
    with open("test.csv", "w") as outfile:
      writer = csv.writer(outfile)
      writer.writerow(my_dict.keys())
      writer.writerows(zip(*my_dict.values()))
    

    Output:

    loss,accuracy,val_loss,val_accuracy
    0.411,0.996,0.147,0.99
    0.115,0.997,0.1,0.99
    0.147,0.997,0.187,0.99
    0.078,0.997,0.065,0.996
    0.032,0.997,0.052,0.996
    

    If you need the dictionary sorted:

    keys = sorted(my_dict.keys())
    with open("test.csv", "w") as outfile:
       writer = csv.writer(outfile, delimiter = ",")
       writer.writerow(keys)
       writer.writerows(zip(*[my_dict[key] for key in keys]))
    

    Sorted Output:

    accuracy,loss,val_accuracy,val_loss
    0.996,0.411,0.99,0.147
    0.997,0.115,0.99,0.1
    0.997,0.147,0.99,0.187
    0.997,0.078,0.996,0.065
    0.997,0.032,0.996,0.052
    

    More on zip.