- DictReader
CSV to dictionary
examples/csv/read_monty.py
import sys import csv if len(sys.argv) != 2: sys.stderr.write("Usage: {} FILENAME\n".format(sys.argv[0])) exit() filename = sys.argv[1] with open(filename) as fh: rd = csv.DictReader(fh, delimiter=',') for row in rd: print(row)
{'lname': 'Chapman', 'born': '8 January 1941', 'fname': 'Graham'} {'lname': 'Idle', 'born': '29 March 1943', 'fname': 'Eric'} {'lname': 'Gilliam', 'born': '22 November 1940', 'fname': 'Terry'} {'lname': 'Jones', 'born': '1 February 1942', 'fname': 'Terry'} {'lname': 'Cleese', 'born': '27 October 1939', 'fname': 'John'} {'lname': 'Palin', 'born': '5 May 1943', 'fname': 'Michael'}