1

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?

2
  • 1
    Can you show an example of how the CSV should like? Commented Feb 27, 2020 at 18:18
  • @HarshalParekh I have updated my question with the CSV file format. Commented Feb 27, 2020 at 18:23

2 Answers 2

2
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.

Sign up to request clarification or add additional context in comments.

3 Comments

What do you mean with "If you don't care about the order"? Thanks.
I assumed my_dict.keys() would reorder, but it didn't. Just in case you need the output sorted, I will add another snippet.
Thanks a lot for your help.
1

The reason you are getting all those AttributeError exceptions is that you aren't using the DictWriter version of writerow() correctly. That version of writerow takes a dictionary as an argument, where the keys of the dictionary are the names of the row fields, and the values of the dictionary are the row field values. That means that row i of your CSV file (not counting the header), should be written something like this:

w.writerow({'loss': my_dict['loss'][i],
            'accuracy': my_dict['accuracy'][i],
            'val_loss': my_dict['val_loss'][i],
            'val_accuracy': my_dict['val_accuracy'][i]})

Since this is kind of clumsy, Harshal Parekh's approach to writing the CSV file is better, though his first approach does rely on Python ordering its dictionary entries a certain way. (Older versions of Python would need to to have my_dict to be of type OrderedDict for that first approach to work.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.