Files
courses/py3interm/EXAMPLES/csv_dictreader.py
T
2025-05-20 11:57:43 -04:00

12 lines
515 B
Python

#!/usr/bin/env python
import csv
field_names = ['term', 'firstname', 'lastname', 'birthplace', 'state', 'party'] # <1>
with open('../DATA/presidents.csv') as presidents_in:
rdr = csv.DictReader(presidents_in, fieldnames=field_names) # <2>
for row in rdr: # <3>
print('{:25s} {:12s} {}'.format(row['firstname'], row['lastname'], row['party'])) # <4>
# string .format can use keywords from an unpacked dict as well:
# print('{firstname:25s} {lastname:12s} {party}'.format(**row))