-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython_template_csv.py
107 lines (78 loc) · 2.95 KB
/
python_template_csv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import io
import logging
import csv
from dataclasses import dataclass, field
# cSpell:ignore datefmt levelname surrogateescape csvreader csvfile
# get logger of this library __name__ and attach a null handler
logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())
@dataclass
class Report:
file : str
records: list = field(default_factory=list, init=False)
def __post_init__(self):
pass
@dataclass
class Record:
row_number: int
def __post_init__(self):
if not isinstance(self.row_number, int):
self.row_number = int(str(self.row_number).strip())
def parse(file, callback=None):
# iterate through rows of an Excel spreadsheet
if callback is not None and not callable(callback):
raise ValueError(
f'callback given but it it not callable. It is a {type(callback)}.'
)
r = Report(file=file)
encoding = 'utf-8'
with open(file=file, encoding=encoding) as csvfile:
# with open(file=file, encoding=encoding, errors='surrogateescape') as csvfile:
csvreader = csv.reader(csvfile, delimiter='\t')
for row_number, row in enumerate(csvreader, 1):
if row_number <= 3:
logger.debug(
f"Row {row_number}. First three rows are ignored. Skipping.")
continue
elif len(row) == 0:
logger.debug(
f"Row {row_number}. Length of row is zero. Skipping.")
continue
x = Record(
row_number=row_number,
)
# run callback at record that's just been created.
# you may want to run callback after r report has been finalized.
if callback is not None and callable(callback):
callback(x)
else:
r.records.append(x)
if callback is None:
return r
def main():
pass
if __name__ == "__main__":
# https://qiita.com/jack-low/items/91bf9b5342965352cbeb
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
# logger setup:
# if this library is run as a script, these logger setup is used
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
file_name = str(sys.argv[0])[:-3] + ".log"
handler_file = logging.FileHandler(file_name)
handler_file.setLevel(logging.DEBUG)
formatter_file = logging.Formatter(
"%(asctime)s - %(filename)s: %(lineno)s: %(funcName)s - %(levelname)s: %(message)s"
)
handler_file.setFormatter(formatter_file)
logger.addHandler(handler_file)
# console logger to show INFO messages
handler_console = logging.StreamHandler()
handler_console.setLevel(logging.INFO)
formatter_console = logging.Formatter("%(name)s: %(levelname)s %(message)s")
handler_console.setFormatter(formatter_console)
logger.addHandler(handler_console)
main()