ziron321
ziron321

Reputation: 23

Read CSV into dictionary, using one column as dict index

I am trying to read this CSV file into a dictionary, so later I can quickly reference the data:

ID,Field1,Field2,Field3
1,DataF1,DataF2,DataF3
2,MoreDataF1,MoreDataF2,MoreDataF3
3,SomeMoreDataF1,SomeMoreDataF2,SomeMoreDataF3

The "ID" value is unique in the file, so I want to use that as the index for a dictionary, whose elements will be dictionaries with the other values. I want to be able to reference myDict[2]["Field3"] and retrieve MoreDataF3

What is the most pythonic way of achieving this?

I was able to do it like this:

import csv

with open("file.csv", mode='r') as csvfile:
    csv_reader = csv.reader(csvfile.readlines(), delimiter=',')

next(csv_reader)

myDict = {}
for row in csv_reader:
    myDict[int(row[0])] = {'Field1': row[1], 'Field2': row[2], 'Field3': row[3]}

But this quickly becomes horrible as the number of fields increase and can't be dynamic. I also played around with DictReader as well but it ended up being even more cumbersome. I believe there must be a better (and nicer way).

Upvotes: 1

Views: 1947

Answers (2)

snakecharmerb
snakecharmerb

Reputation: 55699

You could do this using a DictReader, and a dict comprehension.

import csv 
import pprint


with open('file.csv', newline='') as f:
    reader = csv.DictReader(f)
    MyDict = {int(row.pop('ID')): row for row in reader}


pprint.pprint(MyDict)

Output:

{1: {'Field1': 'DataF1', 'Field2': 'DataF2', 'Field3': 'DataF3'},
 2: {'Field1': 'MoreDataF1', 'Field2': 'MoreDataF2', 'Field3': 'MoreDataF3'},
 3: {'Field1': 'SomeMoreDataF1',
     'Field2': 'SomeMoreDataF2',
     'Field3': 'SomeMoreDataF3'}}

The dict comprehension removes the ID key/value pair from each row using dict.pop to create the key, and uses the remainder of the row dict for the value.

Upvotes: 1

Alexandr Shurigin
Alexandr Shurigin

Reputation: 3981

You can read it using pandas easily

import pandas as pd

csv = pd.read_csv('file.csv')

print(csv)

outputs

python test.py
   ID          Field1          Field2          Field3
0   1          DataF1          DataF2          DataF3
1   2      MoreDataF1      MoreDataF2      MoreDataF3
2   3  SomeMoreDataF1  SomeMoreDataF2  SomeMoreDataF3

More info at https://pandas.pydata.org/

Upvotes: 0

Related Questions