python-mastery/Exercises/soln2_6.md
2023-07-16 20:21:00 -05:00

499 B

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