67

I have a csv file

col1, col2, col3
1, 2, 3
4, 5, 6

I want to create a list of dictionary from this csv.

output as :

a= [{'col1':1, 'col2':2, 'col3':3}, {'col1':4, 'col2':5, 'col3':6}]

How can I do this?

falsetru
  • 357,413
  • 63
  • 732
  • 636
veena
  • 815
  • 2
  • 7
  • 11

8 Answers8

110

Use csv.DictReader:

import csv

with open('test.csv') as f:
    a = [{k: int(v) for k, v in row.items()}
        for row in csv.DictReader(f, skipinitialspace=True)]

Will result in :

[{'col2': 2, 'col3': 3, 'col1': 1}, {'col2': 5, 'col3': 6, 'col1': 4}]
idjaw
  • 25,487
  • 7
  • 64
  • 83
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • 5
    For the lazy, from the linked page - `skipinitialspace`: When `True`, whitespace immediately following the delimiter is ignored. – Flash Mar 07 '17 at 12:45
  • 2
    @falsetru, Here why the output is not coming in this order? [{'col1':1, 'col2':2, 'col3':3}, {'col1':4, 'col2':5, 'col3':6}] – V-T Dec 08 '17 at 04:22
  • @V-T, Dictionary in python 3.6- is not an ordered collection. – falsetru Dec 08 '17 at 07:28
  • 3
    @V-T, If you use Python 3.6+, OrderedDict which guarantee order: https://docs.python.org/3/library/csv.html#csv.DictReader – falsetru Dec 08 '17 at 07:29
  • For unicode support on python2.7 you could use `import unicodecsv as csv`. – dimid Oct 28 '19 at 12:24
  • @falsetru: I see in your [linked docs](https://docs.python.org/3/library/csv.html#csv.DictReader) that as of Python 3.8 the return type is once again `dict`. Is there a way to explicitly enforce an order? – hpy Jun 10 '20 at 22:36
  • 2
    @hpy, Since Python 3.7+, insertion order of dict is guaranteed. https://mail.python.org/pipermail/python-dev/2017-December/151283.html => Don't worry, just use DictReader :) – falsetru Jun 10 '20 at 22:41
  • Whew, that's great to know. Thank you @falsetru! I wonder why that isn't mentioned in the documentation somewhere (or I probably missed it...)... – hpy Jun 11 '20 at 10:18
  • 1
    @hpy, It's mentioned in ["What's New In Python 3.7 - Summary - Release Hightlights"](https://docs.python.org/3/whatsnew/3.7.html#summary-release-highlights) > Python data model improvements: > the insertion-order preservation nature of dict objects has been declared to be an official part of the Python language spec. – falsetru Jun 11 '20 at 13:22
  • 1
    @hpy, also in [Built-in Types - `dict`](https://docs.python.org/3/library/stdtypes.html#dict) > "Dictionaries preserve insertion order. Note that updating a key does not affect the order. Keys added after deletion are inserted at the end.", "Changed in version 3.7: Dictionary order is guaranteed to be insertion order. This behavior was an implementation detail of CPython from 3.6." – falsetru Jun 11 '20 at 13:23
38

Another simpler answer:

import csv
with open("configure_column_mapping_logic.csv", "r") as f:
    reader = csv.DictReader(f)
    a = list(reader)
    print a
The6thSense
  • 8,103
  • 8
  • 31
  • 65
Simon
  • 478
  • 4
  • 4
10

Using the csv module and a list comprehension:

import csv
with open('foo.csv') as f:
    reader = csv.reader(f, skipinitialspace=True)
    header = next(reader)
    a = [dict(zip(header, map(int, row))) for row in reader]
print a    

Output:

[{'col3': 3, 'col2': 2, 'col1': 1}, {'col3': 6, 'col2': 5, 'col1': 4}]
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
5

Answering here after long time as I don't see any updated/relevant answers.

df = pd.read_csv('Your csv file path')  
data = df.to_dict('records')
print( data )
Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
2
# similar solution via namedtuple:    

import csv
from collections import namedtuple

with open('foo.csv') as f:
  fh = csv.reader(open(f, "rU"), delimiter=',', dialect=csv.excel_tab)
  headers = fh.next()
  Row = namedtuple('Row', headers)
  list_of_dicts = [Row._make(i)._asdict() for i in fh]
MOCKBA
  • 1,680
  • 11
  • 19
1

Well, while other people were out doing it the smart way, I implemented it naively. I suppose my approach has the benefit of not needing any external modules, although it will probably fail with weird configurations of values. Here it is just for reference:

a = []
with open("csv.txt") as myfile:
    firstline = True
    for line in myfile:
        if firstline:
            mykeys = "".join(line.split()).split(',')
            firstline = False
        else:
            values = "".join(line.split()).split(',')
            a.append({mykeys[n]:values[n] for n in range(0,len(mykeys))})
user3030010
  • 1,767
  • 14
  • 19
1

Simple method to parse CSV into list of dictionaries

with open('/home/mitul/Desktop/OPENEBS/test.csv', 'rb') as infile:
  header = infile.readline().split(",")
  for line in infile:
    fields = line.split(",")
    entry = {}
    for i,value in enumerate(fields):
      entry[header[i].strip()] = value.strip()
      data.append(entry)
Brian H.
  • 854
  • 8
  • 16
Mitul Panchal
  • 592
  • 5
  • 7
0

My solution for your question :

output = []

with open("test.csv", "r") as csv_file:
    data = csv_file.read().split("\n")

text = [data[i].split(",") for i in range(len(data))]

for i in range(1, len(text)) :
    output.append({text[0][num]:text[i][num] for num in range(len(text[i]))})

the output being the list of dico

jedreety
  • 1
  • 3