diff options
Diffstat (limited to 'jimbrella/csv_table.py')
-rw-r--r-- | jimbrella/csv_table.py | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/jimbrella/csv_table.py b/jimbrella/csv_table.py index 15263c6..8827e5f 100644 --- a/jimbrella/csv_table.py +++ b/jimbrella/csv_table.py @@ -1,5 +1,6 @@ import csv import os +import logging from .lockfile import Lockfile from .utils import identity @@ -45,17 +46,22 @@ class CsvTable: reader = csv.reader(f) rows = [] # `rows` is a list of 2-tuples - for row in reader: + for ln, row in enumerate(reader): # for each tuple (k, v) in `rows`, # it will be unzipped into a dict key-value pair - rows.append( - dict( - [ - (sch["name"], sch["deserializer"](datum)) - for sch, datum in zip(self.schema, row) - ] + try: + rows.append( + dict( + [ + (sch["name"], sch["deserializer"](datum)) + for sch, datum in zip(self.schema, row) + ] + ) ) - ) + except Exception: + logging.warning("%s:%d cannot be read. Skip.", self.path, ln) + continue + f.close() return rows @@ -76,15 +82,13 @@ class CsvTable: except Exception as e: # failure occurred on write # abort write, and write back contents as they were before - # TODO: keep log f.close() f = open(self.path, "w") f.write(backup) raise e finally: f.close() - - self.lockfile.unlock() + self.lockfile.unlock() def _append(self, row) -> list: """Append one row, and return the entire updated table.""" |