Skip to content

Latest commit

 

History

History
25 lines (19 loc) · 499 Bytes

soln2_6.md

File metadata and controls

25 lines (19 loc) · 499 Bytes

Exercise 2.6 - Solution

# reader.py

import csv
from collections import defaultdict

def read_csv_as_dicts(filename, types):
    '''
    Read a CSV file with column type conversion
    '''
    records = []
    with open(filename) as f:
        rows = csv.reader(f)
        headers = next(rows)
        for row in rows:
            record = { name: func(val) for name, func, val in zip(headers, types, row) }
            records.append(record)
    return records

Back