29

I am trying to take input from a CSV file and then push it into a dictionary format (I am using Python 3.x).

I use the code below to read in the CSV file and that works:

import csv

reader = csv.reader(open('C:\\Users\\Chris\\Desktop\\test.csv'), delimiter=',', quotechar='|')

for row in reader:
    print(', '.join(row))

But now I want to place the results into a dictionary. I would like the first row of the CSV file to be used as the "key" field for the dictionary with the subsequent rows in the CSV file filling out the data portion.

Sample data:

     Date        First Name     Last Name     Score
12/28/2012 15:15        John          Smith        20
12/29/2012 15:15        Alex          Jones        38
12/30/2012 15:15      Michael       Carpenter      25

How can I get the dictionary to work?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
gakar06
  • 313
  • 1
  • 4
  • 10
  • from your question it sounds like you want a dictionary (in this case) with 4 keys, `[Date, First Name, Last Name, Score]` and each entry is a list of all the items in the corresponding column. but i have a feeling you mean you want the date on each row to be the key, and then have `[First Name, Last Name, Score]` as the values. – Inbar Rose Dec 30 '12 at 14:12
  • A side note: When you need to write the path explicitly, use normal slashes instead of doubled backslashes -- like `'C:/Users/Chris/Desktop/test.csv'`. It works fine in Windows. Alternatively, you can use a raw string where escape sequences are not interpreted (hence no doubling the backslash) -- `r'C:\Users\Chris\Desktop\test.csv'`. – pepr Dec 30 '12 at 15:15
  • @ Inbar Rose: What I would like to do is create the dictionary with the keys being [Date, First Name, Last Name, and Score]. I would then like the rest of the data to be placed in the value fields but according to the specific keys (heading fields). Meaning Key => Date, Value => 12/28/2012 15:15...and so on. – gakar06 Dec 30 '12 at 16:23
  • @ Inbar Rose: I updated the code from what i had previously and got results i wanted. Now i'd like to try to send it to a CSV file but i don't get the proper format that i'm looking for. It doesn't print each word together but prints out each character per row (if that makes sense). I provided the code above under Version 2. Plus some sample code at the bottom. This code properly shows data sent to a csv file but it is using lists and not dictionaries. Maybe you can help me use that format to get the dictionary to properly export to a csv file. Thanks – gakar06 Jan 02 '13 at 06:53

4 Answers4

68

Create a dictionary, then iterate over the result and stuff the rows in the dictionary. Note that if you encounter a row with a duplicate date, you will have to decide what to do (raise an exception, replace the previous row, discard the later row, etc...)

Here's test.csv:

Date,Foo,Bar
123,456,789
abc,def,ghi

and the corresponding program:

import csv
reader = csv.reader(open('test.csv'))

result = {}
for row in reader:
    key = row[0]
    if key in result:
        # implement your duplicate row handling here
        pass
    result[key] = row[1:]
print(result)

yields:

{'Date': ['Foo', 'Bar'], '123': ['456', '789'], 'abc': ['def', 'ghi']}

or, with DictReader:

import csv
reader = csv.DictReader(open('test.csv'))

result = {}
for row in reader:
    key = row.pop('Date')
    if key in result:
        # implement your duplicate row handling here
        pass
    result[key] = row
print(result)

results in:

{'123': {'Foo': '456', 'Bar': '789'}, 'abc': {'Foo': 'def', 'Bar': 'ghi'}}

Or perhaps you want to map the column headings to a list of values for that column:

import csv
reader = csv.DictReader(open('test.csv'))

result = {}
for row in reader:
    for column, value in row.items():  # consider .iteritems() for Python 2
        result.setdefault(column, []).append(value)
print(result)

That yields:

{'Date': ['123', 'abc'], 'Foo': ['456', 'def'], 'Bar': ['789', 'ghi']}
Phil Frost
  • 3,668
  • 21
  • 29
12

You need a Python DictReader class. More help can be found from here

import csv

with open('file_name.csv', 'rt') as f:
    reader = csv.DictReader(f)
    for row in reader:
        print row
Aamir Rind
  • 38,793
  • 23
  • 126
  • 164
2

Help from @phil-frost was very helpful, was exactly what I was looking for.

I have made few tweaks after that so I'm would like to share it here:

def csv_as_dict(file, ref_header, delimiter=None):

    import csv
    if not delimiter:
        delimiter = ';'
    reader = csv.DictReader(open(file), delimiter=delimiter)
    result = {}
    for row in reader:
        print(row)
        key = row.pop(ref_header)
        if key in result:
            # implement your duplicate row handling here
            pass
        result[key] = row
    return result

You can call it:

myvar = csv_as_dict(csv_file, 'ref_column')

Where ref_colum will be your main key for each row.

-1
import csv
def parser_csv(PATH):
    reader = csv.reader(open("{}.csv".format(PATH), 'r'))
    dict = {}
    list_dict = []
    counter = 0
    for row in reader:
        if counter == 0:
            first_row = row
            ecc = len(first_row)
            counter += 1
        else:
            for col in range(ecc):
                dict.update({first_row[col]:row[col]})
            list_dict.append(dict)
    return list_dict
print(len(parser_csv("path")))
# Have one less csv file (first row is keys of dict)