MCPcopy Create free account
hub / github.com/dabeaz-course/python-mastery / read_csv_as_dicts

Function read_csv_as_dicts

Solutions/3_3/reader.py:5–16  ·  view source on GitHub ↗

Read a CSV file into a list of dicts with column type conversion

(filename, types)

Source from the content-addressed store, hash-verified

3import csv
4
5def 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
18def read_csv_as_instances(filename, cls):
19 '''

Callers

nothing calls this directly

Calls 1

appendMethod · 0.80

Tested by

no test coverage detected