-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdws-to-parqet.py
86 lines (63 loc) · 3.02 KB
/
dws-to-parqet.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
#!/usr/bin/python
import argparse, sys, os, csv
def main():
input_file, output_file = get_files_from_arguments();
input_data = get_data_from_input(input_file);
write_data_to_output(input_data, output_file);
def get_files_from_arguments():
argument_parser = argparse.ArgumentParser();
argument_parser.add_argument('-i', '--input', dest='input', required=True, help='the input-file (CSV) which should be transformed (required)');
argument_parser.add_argument('-o', '--output', dest='output', required=True, help='the output-file (CSV) to store the transformed data (required)');
arguments = argument_parser.parse_args();
input_file = arguments.input;
output_file = arguments.output;
if not input_file.endswith('.csv') or not output_file.endswith('.csv'):
print('the input-file and output-file have to be CSV-files!');
sys.exit(2);
if not os.path.exists(input_file):
print(input_file, 'does not seem to exist!');
sys.exit(2);
if os.path.exists(output_file):
print(output_file, 'already exists. Deleting it...');
os.remove(output_file);
return (input_file, output_file);
# the DWS transaction-overview has the following structure (as of 2022-05-07):
# date; type; portfolio; fonds-name; isin; shares; price per share; amount; currency
def get_data_from_input(input_file):
with open(input_file, newline='') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=';');
# skip header...
next(csv_reader);
data = [];
for row in csv_reader:
entry = {};
entry['date'] = row[0].strip();
entry['type'] = row[1].strip();
entry['isin'] = row[4].strip();
entry['shares'] = row[5].strip();
entry['price'] = row[6].strip();
entry['currency'] = row[8].strip();
data.append(entry);
return data;
def write_data_to_output(data, output_file):
with open(output_file, 'w', newline='') as csv_file:
csv_writer = csv.writer(csv_file, delimiter=';');
# header
csv_writer.writerow(['broker', 'currency', 'date', 'fee', 'isin', 'price', 'shares', 'tax', 'type']);
for entry in data:
# this basically happens when the buy-operation was scheduled but not executed yet - i do not want to have those
if not entry['shares']: continue;
if entry['type'].startswith('Regelm. Kauf'):
type = 'Buy';
elif entry['type'].startswith('Kauf'):
type = 'Buy';
elif entry['type'].startswith('Gutschrift'):
type = 'Dividend';
elif entry['type'].startswith('Verkauf'):
type = 'Sell';
else:
print('Error: Unable to convert DWS type', entry['type']);
continue;
csv_writer.writerow(['DWS', entry['currency'], entry['date'], 0, entry['isin'], entry['price'], entry['shares'], 0, type]);
if __name__ == '__main__':
main();