MCPcopy Create free account
hub / github.com/dabeaz-course/practical-python / parse_csv

Function parse_csv

Solutions/3_7/fileparse.py:4–39  ·  view source on GitHub ↗

Parse a CSV file into a list of records with type conversion.

(filename, select=None, types=None, has_headers=True, delimiter=',')

Source from the content-addressed store, hash-verified

2import csv
3
4def parse_csv(filename, select=None, types=None, has_headers=True, delimiter=','):
5 '''
6 Parse a CSV file into a list of records with type conversion.
7 '''
8 with open(filename) as f:
9 rows = csv.reader(f, delimiter=delimiter)
10
11 # Read the file headers (if any)
12 headers = next(rows) if has_headers else []
13
14 # If specific columns have been selected, make indices for filtering
15 if select:
16 indices = [ headers.index(colname) for colname in select ]
17 headers = select
18
19 records = []
20 for row in rows:
21 if not row: # Skip rows with no data
22 continue
23
24 # If specific column indices are selected, pick them out
25 if select:
26 row = [ row[index] for index in indices]
27
28 # Apply type conversion to the row
29 if types:
30 row = [func(val) for func, val in zip(types, row)]
31
32 # Make a dictionary or a tuple
33 if headers:
34 record = dict(zip(headers, row))
35 else:
36 record = tuple(row)
37 records.append(record)
38
39 return records

Callers

nothing calls this directly

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected