CosimoCD
CosimoCD

Reputation: 3760

Python write a list of dictionaries to csv

I'm stucked in a problem. I have a list of dictionaries that looks like this:

dict_list=[{'alfa':['001','2'], 'beta':['001','3'], 'gamma':['001','2']},
           {'alfa':['002','6'], 'beta':['002','4'], 'omega':['002','7']}]

My intention is to create a csv like this:

WORD, TEXT, FREQ
alfa, 001,  2
alfa, 002,  6
beta, 001,  3
beta, 002,  4
gamma,001,  2
omega,002,  7

Do you think is possible to solve it?

Upvotes: 1

Views: 5867

Answers (4)

chfw
chfw

Reputation: 4592

Here is my penny if I could use pyexcel:

>>> import pyexcel as p
>>> dict_list=[{'alfa':['001','2'], 'beta':['001','3'], 'gamma':['001','2']},
...            {'alfa':['002','6'], 'beta':['002','4'], 'omega':['002','7']}]
>>> s = p.Sheet()
>>> for d in dict_list:
...     s.row += p.get_sheet(adict=d, transpose_after=True)
>>> s.colnames = ['WORD', 'TXT', 'FREQ']
>>> s
pyexcel sheet:
+-------+-----+------+
| WORD  | TXT | FREQ |
+=======+=====+======+
| alfa  | 001 | 2    |
+-------+-----+------+
| beta  | 001 | 3    |
+-------+-----+------+
| gamma | 001 | 2    |
+-------+-----+------+
| alfa  | 002 | 6    |
+-------+-----+------+
| beta  | 002 | 4    |
+-------+-----+------+
| omega | 002 | 7    |
+-------+-----+------+
>>> s.save_as('output.csv')

The output.csv reads like this:

WORD,TXT,FREQ
alfa,001,2
beta,001,3
gamma,001,2
alfa,002,6
beta,002,4
omega,002,7

Upvotes: 1

benjaminmgross
benjaminmgross

Reputation: 2102

pandas provides a very intuitive way to think about iterating over the list of dictionaries. Because each element of the list is a dict that can easily be transformed into a pandas.DataFrame, you can just loop through the list, create a DataFrame for each element, and then concat them all.

In [20]: l = []

In [21]: for dct in dict_list:
    ...:     l.append( 
    ...:         pandas.DataFrame(dct).transpose() 
    ...:     ) 

In [22]: tmp = pandas.concat(l) # aggregate them all
In [23]: print(tmp)
Out[23]:
         0  1
alfa   001  2
beta   001  3
gamma  001  2 
alfa   002  6
beta   002  4
omega  002  7

pandas writes to csv quite easily, so you can just do:

 In [21]: tmp.to_csv('/my-file-path.csv')

With pandas objects you have all sorts ability to then sort them (like the desired result you wanted):

In [24]: tmp.sort_index()
Out[24]:
         0  1
alfa   001  2
alfa   002  6
beta   001  3
beta   002  4
gamma  001  2
omega  002  7

and much, more! To exactly replicate what you were looking for, just rename the Index and the columns, like so:

In [30]: tmp.index.name = 'WORD'
In [36]: tmp = tmp.rename(columns={0: 'TEXT', 1: 'FREQ'})
In [37]: print(tmp)

      TEXT FREQ
WORD
alfa   001    2
beta   001    3
gamma  001    2
alfa   002    6
beta   002    4
omega  002    7

Upvotes: 3

DYZ
DYZ

Reputation: 57033

(Always) use pandas:

import pandas as pd

df0 = pd.DataFrame(dict_list).stack().reset_index()
#    level_0 level_1         0
#0        0    alfa  [001, 2]
#1        0    beta  [001, 3]
#2        0   gamma  [001, 2]
#3        1    alfa  [002, 6]
#4        1    beta  [002, 4]
#5        1   omega  [002, 7]

df0 = pd.concat([df0, df0[0].apply(pd.Series)], axis=1)
df0.columns = ('dummy','WORD','tuple','TEXT','FREQ')
df0[['WORD','TEXT','b']].sort_values('WORD').to_csv("your_file.csv",index=False)
#WORD,TEXT,FREQ
#alfa,001,2
#alfa,002,6
#beta,001,3
#beta,002,4
#gamma,001,2
#omega,002,7

Upvotes: 2

Moses Koledoye
Moses Koledoye

Reputation: 78546

Create a DictWriter object from the file object, and write to the file using the writerows method of the writer object after converting to an iterable of dicts:

import csv

with open(your_filename, 'w') as f:
    fieldnames = ['WORD', 'TEXT', 'FREQ']
    writer = csv.DictWriter(f, fieldnames=fieldnames)
    writer.writeheader()
    writer.writerows([dict(zip(fieldnames, (k,v1,v2))) for d in dict_list 
                                                       for k, [v1, v2] in d.items()])

If you print the iterable before writing to the file, you'll have:

# [{'FREQ': '2', 'TEXT': '001', 'WORD': 'alfa'},
#  {'FREQ': '2', 'TEXT': '001', 'WORD': 'gamma'},
#  {'FREQ': '3', 'TEXT': '001', 'WORD': 'beta'},
#  {'FREQ': '6', 'TEXT': '002', 'WORD': 'alfa'},
#  {'FREQ': '7', 'TEXT': '002', 'WORD': 'omega'},
#  {'FREQ': '4', 'TEXT': '002', 'WORD': 'beta'}]

Upvotes: 2

Related Questions