Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@josephfinlayson
Created March 16, 2018 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josephfinlayson/a347207ded18143517dacf88e3cd51a2 to your computer and use it in GitHub Desktop.
Save josephfinlayson/a347207ded18143517dacf88e3cd51a2 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from __future__ import absolute_import, print_function\n",
"from IPython.core.debugger import Pdb\n",
"from IPython.core.debugger import set_trace\n",
"import datetime\n",
"import itertools as it\n",
"from meza.io import read_csv, IterStringIO\n",
"from meza import io, process, convert\n",
"from csv2ofx import utils\n",
"from csv2ofx.ofx import OFX\n",
"from operator import itemgetter\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"csv_file = '/Users/jfinlays/Downloads/Download.csv'\n",
"# Date\t\n",
"# Native Amount\t\n",
"# Native Currency\t\n",
"# Billed Amount\t\n",
"# Billed Currency\n",
"# Has Folio\t\n",
"# Merchant\t\n",
"# City\t\n",
"# Card Type\t\n",
"# Personal Card\t\n",
"# Card Account\n",
"i = 0\n",
"N26mapping = {\n",
" 'has_header': True, \n",
" 'is_split': False, \n",
" 'bank': 'Bank', \n",
" 'delimiter': ',',\n",
" 'date': itemgetter('Date'),\n",
" 'currency': itemgetter('Type Foreign Currency'),\n",
" 'account': 'N26 Auto Import', \n",
" 'amount': lambda tr: tr['Amount (Foreign Currency)'] or tr['Amount (EUR)'], \n",
" 'desc': lambda tr: 'EUR charged:' + tr['Amount (EUR)'], \n",
" 'notes': lambda tr: 'EUR charged:' + tr['Amount (EUR)'], \n",
" 'payee': itemgetter('Payee'), \n",
" 'class': itemgetter('Category'),\n",
" 'id': itemgetter('Date')\n",
"}\n",
"\n",
"paypal_mapping = {\n",
" 'has_header': True, \n",
" 'is_split': False, \n",
" 'bank': 'Bank', \n",
" 'delimiter': ',',\n",
" 'date': itemgetter('Date'),\n",
" 'currency': 'EUR',\n",
" 'account': 'Paypal Auto Import', \n",
" 'amount': itemgetter('Gross'), \n",
" 'payee': itemgetter('Name'), \n",
" 'class': 'Taxi / Bus',\n",
" 'id': itemgetter('Date')\n",
"}\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"ofx = OFX(paypal_mapping)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'Date': '31/12/2017', ' Time Zone': 'PST', ' Name': 'Intelligent Apps GmbH', ' Type': 'Preapproved Payment Sent', ' Status': 'Completed', ' Currency': 'EUR', ' Gross': '53'}\n",
"[31, 12, 2017]\n"
]
}
],
"source": [
"records = read_csv(csv_file)\n",
"\n",
"records_with_better_date = list()\n",
"for record in list(records):\n",
" print (record)\n",
" dateClone = list(map(lambda x: int(x), record['Date'].split('/')))\n",
" print(dateClone)\n",
" dateInstance = datetime.date(month=dateClone[1], day=dateClone[0], year=dateClone[2])\n",
" americanDate = '/'.join(map(lambda x: str(x), [dateInstance.month, dateInstance.day, dateInstance.year]))\n",
" record['Date'] = americanDate\n",
" records_with_better_date.append(record)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# print (records_with_better_date)\n",
"\n",
"# for record in records"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"groups = ofx.gen_groups(records)\n",
"trxns = ofx.gen_trxns(groups)\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"cleaned_trxns = ofx.clean_trxns(trxns)\n",
"data = utils.gen_data(cleaned_trxns)\n",
"\n",
"content = it.chain([ofx.header(), ofx.gen_body(data), ofx.footer()])\n",
"\n",
"\n",
"for r in ofx.gen_body(data): \n",
" print (r)\n",
" \n",
"lines = list(IterStringIO(content))\n",
"\n",
"with open('out.ofx', \"wb\" ) as file:\n",
" file.writelines(lines)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment