Solution: Dictionary of dictionaries
examples/dictionary/dictionary_of_dictionaries.py
import sys filename = 'examples/csv/monty_python.csv' if len(sys.argv) == 2: filename = sys.argv[1] people = {} with open(filename) as fh: fh.readline() # skip first row for line in fh: line = line.rstrip('\n') fname, lname, born = line.split(',') people[(fname, lname)] = { 'fname': fname, 'lname': lname, 'born': born, } print(people[('Eric', 'Idle')]['born'])
examples/dictionary/dictionary_of_dictionaries_csv.py
import sys import csv filename = 'examples/csv/monty_python.csv' if len(sys.argv) == 2: filename = sys.argv[1] people = {} with open(filename) as fh: reader = csv.DictReader(fh) for line in reader: people[(line['fname'], line['lname'])] = line print(people[('Eric', 'Idle')]['born'])