Home > Enterprise >  How to create CSV from dictionary of dictionaries?
How to create CSV from dictionary of dictionaries?

Time:04-21

I have created a python program to produce a frequency distribution of letters found in a list of words.

The output is shown as a dictionary of dictionaries where the primary key is a letter and the sub-dictionaries are key=letter position with a value of count of that letter in the position.

INPUT:

  mywords = ['APPLE','APPLES','APPLEPIE','LETTER','SIMPLE']

OUTPUT:

  letterCount {'A': {0: 3},
               'P': {1: 3, 2: 3, 5: 1, 3: 1},
               'L': {3: 3, 0: 1, 4: 1},
               'E': {4: 4, 7: 1, 1: 1, 5: 1},
               'S': {5: 1, 0: 1},
               'I': {6: 1, 1: 1},
               'T': {2: 1, 3: 1},
               'R': {5: 1},
               'M': {2: 1}
              }

I am looking to summarize these results into a simple CSV format. Here is an example I manually created:

Letter,Position,Count
A,0,3
P,1,3
P,2,3
P,5,1
P,3,1
L,3,3
L,0,1
L,4,1
E,4,4
E,7,1
E,1,1
E,5,1
S,5,1
S,0,1
I,6,1
I,1,1
T,2,1
T,3,1
R,5,1
M,2,1

CodePudding user response:

Assuming your second dictionary only contains one value each (it could contain a list, but then it gets more complicated), you could do double loop:

letterCount = {'A': {0: 3},
               'P': {1: 3, 2: 3, 5: 1, 3: 1},
               'L': {3: 3, 0: 1, 4: 1},
               'E': {4: 4, 7: 1, 1: 1, 5: 1},
               'S': {5: 1, 0: 1},
               'I': {6: 1, 1: 1},
               'T': {2: 1, 3: 1},
               'R': {5: 1},
               'M': {2: 1}
              }

file = open('my_file.csv', 'w')

for letter_key in letterCount.keys():
    for count_key in letterCount[letter_key]:
        line = "{0}, {1}, {2}\n".format(letter_key, count_key, letterCount[letter_key][count_key])
        file.write(line)

file.close()

It isn't the most ellegant solution, but it gets the job done.

  • Related