Created
January 11, 2012 05:38
-
-
Save vst/1593234 to your computer and use it in GitHub Desktop.
Parses an EBS statement retrieved from secure.ebs.in and produces a simpler representation for bank account management.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################## | |
# Copyright 2012 Vehbi Sinan Tunalioglu # | |
# # | |
# Licensed under the Apache License, Version 2.0 (the "License"); # | |
# you may not use this file except in compliance with the License. # | |
# You may obtain a copy of the License at # | |
# # | |
# http://www.apache.org/licenses/LICENSE-2.0 # | |
# # | |
# Unless required by applicable law or agreed to in writing, software # | |
# distributed under the License is distributed on an "AS IS" BASIS, # | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # | |
# See the License for the specific language governing permissions and # | |
# limitations under the License. # | |
############################################################################## | |
import csv | |
import sys | |
import datetime | |
from decimal import Decimal | |
############# | |
# FUNCTIONS # | |
############# | |
def read_statement(filepath): | |
""" | |
Reads an EBS account statement which you can download from | |
http://secure.ebs.in and returns a list of dictionaries of | |
individual statement lines. | |
Note that the file must be converted to a proper CSV file with the | |
headers on the first line. | |
""" | |
# Open the CSV file: | |
reader = csv.reader(open(filepath)) | |
# Iterate over the lines and construct the return value: | |
header = None | |
statement = list() | |
for line in reader: | |
if not header: | |
header = line | |
else: | |
statement.append(dict(zip(header, line))) | |
# Done. Return with the list of dictionaries as the statement: | |
return statement | |
def item2trans(item): | |
""" | |
Compiles a statement item to a 4-tuple which is a simplified | |
representation of the statement transaction. | |
""" | |
# Parse and compute values: | |
date = datetime.datetime.strptime(item["Date"], | |
"%Y-%m-%d %H:%M:%S").date() | |
amount = -(Decimal(item["Credit"]) + Decimal(item["Debit"])) | |
description = "%s/%s/%s %s, %s, %s, %s" % (item["TransactionID"], | |
item["PaymentID"], | |
item["Merchant Ref. No"], | |
item["Date"], | |
item["AccountID"], | |
item["Particular"], | |
item["PaymentMethod"]) | |
# Done. Return a 4-tuple of required values: | |
return (date, amount, None, description) | |
############# | |
# MAIN LOOP # | |
############# | |
if __name__ == "__main__": | |
# Check the input parameter: | |
if len(sys.argv) <= 2: | |
sys.stderr.write("Usage: " | |
"python parseEBSstatement.py " | |
"[input file path] [output file path]\n") | |
sys.exit(1) | |
# Read the statement and sort transactions: | |
statement = [item2trans(line) for line in read_statement(sys.argv[1])] | |
statement = sorted(statement, key=lambda x: x[3]) | |
# Print for information purposes: | |
for trans in statement: | |
print "%s %10.2f %s" % (trans[0], trans[1], trans[3]) | |
# Write the simplified version: | |
csv.writer(open(sys.argv[2], "w")).writerows([ | |
["Date", "Amount", "Balance", "Description"]] + statement) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment