Parse a CSV file into a list of records with type conversion.
(filename, select=None, types=None, has_headers=True, delimiter=',')
| 2 | import csv |
| 3 | |
| 4 | def 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 |