Skip to content

Instantly share code, notes, and snippets.

@lobeck
Created June 28, 2019 19:43
Show Gist options
  • Save lobeck/ac479531ec41ba27293f09bc29b61b1f to your computer and use it in GitHub Desktop.
Save lobeck/ac479531ec41ba27293f09bc29b61b1f to your computer and use it in GitHub Desktop.
# -*- coding: utf8 -*-
import argparse
import csv
from datetime import datetime
"""
Calculate amount similar to comdirects Wechselgeldsparen out of American Express ofx.csv files
You need to do the transfer on your own, but at least it will calculate the amount.
"""
parser = argparse.ArgumentParser(description='Calculate fractions amount for AMEX CSV statements')
parser.add_argument('input_file')
args = parser.parse_args()
def print_number(number, delimiter=','):
presign = ''
if number < 0:
presign = '-'
digits = [int(d) for d in str(number).replace('-', '')]
if len(digits) <= 2:
return "0{}{}".format(delimiter, ''.join([str(x) for x in digits[-2:]]))
else:
return "{}{}{}{}".format(presign, ''.join([str(x) for x in digits[:-2]]), delimiter,
''.join([str(x) for x in digits[-2:]]))
def get_fraction(number):
digits = [int(d) for d in str(number).replace('-', '')]
return int(''.join([str(x) for x in digits[-2:]]))
def get_fraction_difference(number):
diff = 100 - get_fraction(number)
if diff == 100:
return 0
else:
return diff
with open(args.input_file, 'r') as f:
csvreader = csv.reader(f)
min_date = datetime.now()
max_date = datetime(2019, 1, 1, 0, 0, 0)
skipped = 0
fractions = 0
for row in csvreader:
(transaction_date, reference, amount, subject, booking_date, unknown) = tuple(row)
transaction_date = datetime.strptime(transaction_date, "%d/%m/%Y")
amount = int(amount.strip().replace(",", "").replace(".", ""))
if transaction_date < min_date:
min_date = transaction_date
if transaction_date > max_date:
max_date = transaction_date
fraction_difference = get_fraction_difference(amount)
if amount > 0:
fractions += fraction_difference
print("{} - {:<40} - {:>8} - {:>8} - {:>2} ct{}".format(transaction_date.strftime("%d.%m.%Y"), subject, amount,
print_number(amount), fraction_difference,
" - skipped" if amount <= 0 else ""))
print("")
print(
"{} - {} - {} €".format(min_date.strftime("%d.%m.%Y"), max_date.strftime("%d.%m.%Y"), print_number(fractions)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment