forked from dabeaz-course/practical-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileparse.py
More file actions
44 lines (38 loc) · 1.09 KB
/
fileparse.py
File metadata and controls
44 lines (38 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# fileparse.py
#
# Exercise 3.3
import csv
def parse_csv(filename, select=None, types=None, has_headers=True, delimiter=','):
'''
Parse a CSV file into a list of records
'''
if select and not has_headers:
raise RuntimeError("select argument requires column headers")
with open(filename) as f:
rows = csv.reader(f, delimiter=delimiter)
if has_headers:
# Read the file headers
headers = next(rows)
if select:
indices = [headers.index(col) for col in select]
headers = select if has_headers else None
else:
indices = []
records = []
for rowno, row in enumerate(rows, 1):
if not row:
continue
if indices:
row = [ row[idx] for idx in indices]
if types:
try:
row = [func(val) for func, val in zip(types, row) ]
except ValueError as e:
print(f"Row {rowno}: Couldn't convert {row}")
print(f"Row {rowno}: Reason {e}")
if has_headers:
record = dict(zip(headers, row))
records.append(record)
else:
records.append(row)
return records