-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
93 lines (76 loc) · 2.13 KB
/
main.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
import sys
import csv
import os.path
from os import path
# check if file exists
def file_exists(filename):
return path.exists(filename)
# extract banks from src file
def retrieve_banks(filename):
with open(filename, newline='') as csvfile:
table = csv.reader(csvfile, delimiter = ',', quotechar='"')
banks = []
for row in table:
banks.append(row[0])
return banks
def retrieve_bank_regs(filename):
with open(filename, newline='') as csvfile:
table = csv.reader(csvfile, delimiter = ',', quotechar='"')
bank_regs = {}
for row in table:
bank_regs[row[1]] = row[0]
return bank_regs
# add date to lines
def append_file(lines, filename):
with open(filename, newline='') as csvfile:
table = csv.reader(csvfile, delimiter = ',', quotechar='"')
line_iter = iter(lines)
for row in table:
col_count = 0
for col in list(row)[1:]:
line = next(line_iter)
line.append(col if len(col) > 0 else 0)
col_count += 1
return lines
def usage():
print('')
print('Usage: python3 main.py <src_filename>')
print('')
print('Example:')
print('\tpython3 main.py src_sovensprocent.csv')
print('Expected output:')
print('\tNEW FILE: out_src_solvensprocent.csv')
def main():
# expect input from commandline
if len(sys.argv) < 2:
print(f'ERROR: incorrect number of input parameters. ')
usage()
exit(1)
src = sys.argv[1]
# check if file exists, else fail
if not file_exists(src):
print(f'ERROR: specified file "{src}" does not exist. Check filename and try again.')
print('Exiting.')
exit(1)
# prepare program
lines = []
bank_regs = retrieve_bank_regs('bank-reg-nr.csv')
banks = retrieve_banks(src)
years = range(2000, 2020)
# iterate all banks and years
for bank in banks:
for year in years:
line = [bank_regs[bank], bank, year]
lines.append(line)
# add data
lines = append_file(lines, src)
# output to file
with open("out_" +src, "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(lines)
# complete run
print('Done.')
exit(0)
# start executing
if __name__== "__main__":
main()