-
Notifications
You must be signed in to change notification settings - Fork 2
/
csv-dbs.py
executable file
·128 lines (107 loc) · 4.74 KB
/
csv-dbs.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Converts DBS Bank's savings account CSV output into a proper CSV file for input to personal finance applications.
#
# This runs on Python 3.9
#
# Copyright (c) 2018-2022, Sasmito Adibowo
# https://indiespark.top
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import sys
import os.path
import csv
import datetime
OUTPUT_DATE_FORMAT = '%Y-%m-%d'
INPUT_DATE_FORMAT = '%d %b %Y'
def reformat_date(date_str):
date_obj = datetime.datetime.strptime(date_str, INPUT_DATE_FORMAT)
return date_obj.strftime(OUTPUT_DATE_FORMAT)
def run_convert(input_file_name, output_file_name):
# DBS's CSV file format starts with 16 lines of blanks and header information containing the following:
# - the account number
# - account balances
# - header information
# - statement date
#
with open(input_file_name, 'r') as input_file, open(output_file_name, 'w') as output_file:
csv_input = csv.reader(input_file)
csv_output = csv.writer(output_file)
# Skip until the header row
for input_row in csv_input:
if len(input_row) > 0 and input_row[0] == 'Transaction Date':
break
csv_output.writerow(['Transaction_Date', 'Withdrawal', 'Deposit', 'Type', 'Ref1', 'Ref2', 'Ref3'])
transaction_ref1_str = None
transaction_ref2_str = None
transaction_ref3_str = None
for input_row in csv_input:
if len(input_row) == 0:
continue
if len(input_row) >= 4:
transaction_date_str = input_row[0].strip()
transaction_ref_str = input_row[1].strip()
debit_amt_str = input_row[2].strip()
credit_amt_str = input_row[3].strip()
if len(input_row) >= 5:
transaction_ref1_str = input_row[4].strip()
if len(input_row) >= 6:
transaction_ref2_str = input_row[5].strip()
if len(input_row) >= 7:
transaction_ref3_str = input_row[6].strip()
date_str = reformat_date(transaction_date_str)
csv_output.writerow([
date_str,
debit_amt_str,
credit_amt_str,
transaction_ref_str,
transaction_ref1_str,
transaction_ref2_str,
transaction_ref3_str
])
def print_help():
script_name = os.path.basename(__file__)
text = ("\nReformat DBS Savings Account pseudo-CSV export into a proper CSV file.\n"
"Usage:\n"
"\t {0} {{input-file}} {{output-file}}\n"
"Where:\n"
"{{input-file}}\tThe file obtained from DBS' `Export to CSV` function (the `download` button on the balance history screen).\n"
"{{output-file}}\tWhere to write the properly-formatted CSV output file.\n"
).format(script_name)
print(text)
if __name__ == '__main__':
if len(sys.argv) != 3:
print_help()
exit(2)
input_file = sys.argv[1]
output_file = sys.argv[2]
if not os.path.isfile(input_file):
print(f"Input file does not exists: {input_file}")
print_help()
exit(3)
if os.path.isfile(output_file):
print(f"Output file already exists: {output_file}")
print_help()
exit(3)
run_convert(input_file, output_file)
exit(0)