diff --git a/ex5.5.py b/ex5.5.py new file mode 100644 index 0000000..012ef2a --- /dev/null +++ b/ex5.5.py @@ -0,0 +1,6 @@ +import logging + +from reader import read_csv_as_dicts + +logging.basicConfig(level=logging.DEBUG) +port = read_csv_as_dicts("Data/missing.csv", types=[str, int, float]) diff --git a/reader.py b/reader.py index 1082957..9b7c465 100644 --- a/reader.py +++ b/reader.py @@ -1,4 +1,5 @@ import csv +import logging from typing import Any, Callable, Iterable, Mapping, Optional, Sequence @@ -10,7 +11,15 @@ def convert_csv( rows = csv.reader(lines) if headers is None: headers = next(rows) - records = list(map(lambda row: conv(row, headers), rows)) + records = [] + for n, row in enumerate(rows): + try: + records.append(conv(row, headers)) + except ValueError as ex: + log = logging.getLogger(__name__) + log.warning(f"Row {n}: bad row: {row}") + log.debug(f"Reason: {ex}") + return records