-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathubs-ynab-formatter.py
88 lines (66 loc) · 2.92 KB
/
ubs-ynab-formatter.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
import csv
import re
import datetime
class aTransaction:
def __init__(self, date, payee, memo, amount):
self.date = date
self.payee = payee
self.memo = memo
self.amount = amount
transactions = []
# optional user setting prompts
print('Remove Twint prefix from payee field? (Y/n):')
remove_twint = input()
print('Remove PayPal prefix from payee field? (Y/n):')
remove_paypal = input()
print('Remove country suffix from payee field? (Y/n):')
remove_country_code = input()
print('Remove multiple spaces from payee field? (Y/n):')
remove_multiple_spaces = input()
print('Add Cardholder name to memo field? (Y/n):')
add_cardholder_to_memo = input()
with open('transactions.csv', newline='', errors='replace') as csvfile:
spamreader = csv.reader(csvfile, delimiter=';')
for row in spamreader:
if len(row) != 0:
account_number_match = re.search("^\d{4}\s{1}\d{4}\s{1}\d{4}$", row[0])
if account_number_match:
transaction_payee = row[4]
transaction_memo = row[5]
transaction_amount = 0
# format transaction amount
if row[10] != '' and row[11] == '':
transaction_amount = "-" + row[10]
elif row[10] == '' and row[11] != '':
transaction_amount = row[11]
else:
print("Invalid transaction amount: row 10: ", str(row[10]), "row 11: ", str(row[11]))
# remove Twint prefix if requested
if remove_twint == 'Y':
transaction_payee = transaction_payee.replace('TWINT *','')
# remove PayPal prefix if requested
if remove_paypal == 'Y':
transaction_payee = transaction_payee.replace('PAYPAL *','')
# remove country code suffix if requested
if remove_country_code == 'Y':
transaction_payee = re.sub("\s[A-Z]{3}$", "", transaction_payee)
# remove multiple spaces if requested
if remove_country_code == 'Y':
transaction_payee = re.sub("\s{2,}", " ", transaction_payee)
# add cardholder name to memo field if requested
if add_cardholder_to_memo == 'Y':
transaction_memo = transaction_memo + " " + row[2]
# remove trailing spaces from payee
transaction_payee = re.sub("\s$", "", transaction_payee)
# format date
x = datetime.datetime.strptime(row[3], '%d.%m.%Y')
transaction_date = x.strftime('%Y-%m-%d')
transaction = aTransaction(transaction_date, transaction_payee, transaction_memo, transaction_amount)
transactions.append(transaction)
# write to output csv file
with open('output.csv', 'w', newline='') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=';', quoting=csv.QUOTE_ALL)
spamwriter.writerow(["Date","Payee","Memo","Amount"])
for transaction in transactions:
print(transaction.date, transaction.payee, transaction.memo, transaction.amount)
spamwriter.writerow([transaction.date, transaction.payee, transaction.memo, transaction.amount])