MCPcopy Create free account
hub / github.com/dabeaz-course/python-mastery / CSVParser

Class CSVParser

Solutions/5_2/reader.py:8–27  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6log = logging.getLogger(__name__)
7
8class CSVParser(ABC):
9
10 def parse(self, filename):
11 records = []
12 with open(filename) as f:
13 rows = csv.reader(f)
14 headers = next(rows)
15 for rowno, row in enumerate(rows, start=1):
16 try:
17 record = self.make_record(headers, row)
18 records.append(record)
19 except ValueError as e:
20 log.warning('Row %d: Bad row: %s', rowno, row)
21 log.debug('Row %d: Reason: %s', rowno, e)
22
23 return records
24
25 @abstractmethod
26 def make_record(self, headers, row):
27 raise NotImplementedError()
28
29class DictCSVParser(CSVParser):
30 def __init__(self, types):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected