Read a CSV file into a list of dicts with column type conversion
(filename, types)
| 3 | import csv |
| 4 | |
| 5 | def read_csv_as_dicts(filename, types): |
| 6 | ''' |
| 7 | Read a CSV file into a list of dicts with column type conversion |
| 8 | ''' |
| 9 | records = [] |
| 10 | with open(filename) as f: |
| 11 | rows = csv.reader(f) |
| 12 | headers = next(rows) |
| 13 | for row in rows: |
| 14 | record = { name: func(val) for name, func, val in zip(headers, types, row) } |
| 15 | records.append(record) |
| 16 | return records |
| 17 | |
| 18 | |
| 19 |