1

I have a csv file that looks like:

a, b, c, d
1, 1, 2, 2
2, 3, 3, 4
2, 2, 1, 1

I'd like to load this csv file into a dictionary so that I can get

dict['a'] = 1, 2, 2
dict['b'] = 1, 3, 2
dict['c'] = 2, 3, 1
dict['d'] = 2, 4, 1

Is there a way to do this right at the csv reader level?

I got this far:

    import csv

    headers = {}

    with open('file.csv') as csvfile:

        reader = csv.reader(csvfile, delimiter = ',')
        count = 0;
        for row in reader:
            if count == 0:
                for field in row:
                    if field not in headers.keys():
                        headers[field] = []
            count +=1   

This loads the header and now I'd like to load each value in.

DYZ
  • 55,249
  • 10
  • 64
  • 93
Zee Fer
  • 339
  • 4
  • 14

3 Answers3

3

You could use a DictReader. However, if the input file contains spaces around the field values, they need to be stripped manually.

import csv

with open('file.csv') as csvfile:
    reader = csv.DictReader(csvfile, skipinitialspace=True)
    d = {name: [] for name in reader.fieldnames}
    for row in reader:
        for name in reader.fieldnames:
            d[name].append(row[name])
    print(d)

Thanks to @temporalwolf for the Dialect.skipinitialspace trick!

janos
  • 120,954
  • 29
  • 226
  • 236
  • or you can use [`skipinitialspace=True`](https://docs.python.org/3/library/csv.html#csv.Dialect.skipinitialspace) and not do all that manually: "When True, whitespace immediately following the delimiter is ignored." – TemporalWolf Jul 19 '17 at 19:15
  • You don't even have to make a dialect for it: `csv.DictReader(csvfile, skipinitialspace=True)` is sufficient :) – TemporalWolf Jul 19 '17 at 19:21
  • @TemporalWolf nice nice nice – janos Jul 19 '17 at 19:23
  • If I use skipInitialSpace=True, then I don't have any 'fieldnames' for some reason (empty list), but if I don't set it to true, then I have this issue: https://stackoverflow.com/questions/31225128/csv-reader-is-separating-values-by-individual-character where the CSV text is parsed character-by-character incorrectly. Also, be careful if you're debugging this solution -- `d` in PDB is used to select which frame you're on for the stack: https://docs.python.org/3/library/pdb.html#pdbcommand-down – Raleigh L. Jun 24 '22 at 07:09
2

try using pandas:

import pandas as pd

df = pd.read_csv('YOUR_PATH_HERE')

your_dict = df.to_dict(orient='list')

output:

{'a': [1, 2, 2], ' b': [1, 3, 2], ' c': [2, 3, 1], ' d': [2, 4, 1]}

In addition, Pandas Dataframes could be a really good tool instead of using a dictionary. Here is the documentaion on to dict

MattR
  • 4,887
  • 9
  • 40
  • 67
  • You want OP to import a 3rd-party library just to read a CSV file? – errantlinguist Jul 19 '17 at 19:09
  • 2
    look at the readability behind pandas... very clear and understandable. I also suggested that this can be a powerful tool beyond reading csv files. MY assumption is if there is a reason behind putting the csv into a dictionary, I'm willing to bet that a DataFrame object can handle the data wrangling in a much easier/faster way. The OP did not state the reason behind his need, therefore I am assuming there is more than "i need a dicitonary of my file" – MattR Jul 19 '17 at 19:13
2

You can try this, simply using the builtin csv module and dictionary comprehension:

import csv

data = list(csv.reader(open('file.csv')))

final_data = {i[0]:map(int, i[1:]) for i in zip(*data)}

Output:

{'a': [1, 2, 2], ' b': [1, 3, 2], ' d': [2, 4, 1], ' c': [2, 3, 1]}
Ajax1234
  • 69,937
  • 8
  • 61
  • 102