与捷克Raiffeisen银行文本银行对账单交互的库
rbcz的Python项目详细描述
rbcz.py
rbcz是用于分析纯文本银行语句的python库 那家Raiffeisen银行通过电子邮件发送出去的。它向 解析存储在本地文件系统中的语句或搜索 通过你的电子邮件并通过imap检索它们。
安装
使用pip从pypi检索:
$ pip install rbcz
或者克隆此repo,并使用setup.py:
安装$ git clone https://github.com/smcl/rbcz.py $ cd rbcz.py $ python setup.py install
方法
有三个简单的函数-read_statement, read_statements和read_statements_from_imap。解析单个 语句我们可以使用read_statement函数,它接受 单参数-本地银行对账单的路径 文件系统-并返回一个Statement对象:
from rbcz import * statement = rbcz.read_statement("/path/to/stmt_january_czk.txt")
如果我们在本地有很多语句,我们可以使用read_statements 它接受要解析的文件名列表,并返回 Statement:
from rbcz import * statement_filenames = [ "stmt_jan_czk.txt", "stmt_feb_czk.txt", "stmt_mar_czk.txt" ] statements = rbcz.read_statements(statement_filenames)
如果我们没有将所有语句存储在本地,我们可以使用 read_statements_from_imap连接到IMAP服务器并搜索它 对于来自“info@rb.cz”地址的电子邮件,请下载并解析 附件并返回Statement的列表。
from rbcz import * statements = read_statements_from_imap("imap.gmail.com", "my.email.address@gmail.com", "password123", "inbox")
类型
有两种类型-Statement和Movement。
语句
aStatement表示月结单:
- account_name-(string)主帐户持有人(您的 名字!)
- account_number-(string)您的帐号
- iban-(string)您帐户的IBAN
- currency-(string)帐户持有的货币
- number-(int)语句的编号(第一个语句 将是1)
- from_date-(datetime)语句的开始日期
- to_date-(datetime)语句的结束日期
- opening_balance-(十进制)期初余额 声明
- income-(十进制)您在 报表的报告期
- expenses-(十进制)您在 报表的报告期
- closing_balance-(十进制)截止日期的余额 声明
- blocked-(十进制)为付款设置了金额限制
- receivable-(十进制)已收到但尚未清除/结算的金额
- available_balance-(十进制)可用的金额 在结算日提取
- movements-(移动列表)单个现金移动 报告期内的(进出付款)
移动
Movement是一个单独的事务-例如ATM 提款或借记卡付款。每个Statement都有一个 Movement为 报告期。每个Movement都有以下内容:*number- (int)当前语句中移动的id*amount- (十进制)事物的数量*date_deducted-(datetime)日期 事务最初是提交的*date_completed- (date time)交易完成的日期+时间* counterparty_account_number-(string)付款的帐户是 发送到*^{TT41}$-(字符串)或从*^{TT41}$-(字符串)接收 有关付款发送或接收的帐户的信息, 如果可用*narrative-(字符串)有关 事务*transaction_type-(string)什么类型的 发生事务*specific_symbol-(字符串)特定符号 用于移动*variable_symbol-(字符串)变量符号 移动*constant_symbol-(字符串)移动的常量符号
示例
下面的脚本将尝试分析 ./rb目录,然后取期末余额和高/低水位 每个周期的标记并绘制在图表上。
#!/usr/bin/python # system/lib imports import os import numpy as np import matplotlib.pyplot as plt from matplotlib.dates import YearLocator, MonthLocator, DateFormatter, drange, date2num from numpy import arange # rbcz library from rbcz import * # load and sort the statements statements = sorted( rbcz.read_statements([ "./rb/" + f for f in os.listdir("./rb") ]), key=lambda stmt: stmt.from_date) # function to deterine high/low-water mark on account def high_low_water(stmt): bal = stmt.opening_balance hwm = bal lwm = bal for m in stmt.movements: bal += m.amount if bal > hwm: hwm = bal if bal < lwm: lwm = bal return (lwm, hwm) #plt.gca().set_color_cycle(['green', 'black', 'red']) # extract high/low-water marks water_marks = [ high_low_water(s) for s in statements ] low_water_marks = [ wm[0] for wm in water_marks ] high_water_marks = [ wm[1] for wm in water_marks ] # extract closing balance and dates closing_balances = [ s.closing_balance for s in statements ] dates = date2num([ s.from_date for s in statements ]) # prepare and display the chart using matplotlib y = arange(len(dates)*1.0) # plot the data fig, ax = plt.subplots() ax.set_color_cycle(['green', 'black', 'red']) ax.plot_date(dates, high_water_marks, "o-") ax.plot_date(dates, closing_balances, "o-") ax.plot_date(dates, low_water_marks, "o-") # fix up the axes ax.xaxis.set_major_locator(YearLocator()) ax.xaxis.set_minor_locator(MonthLocator()) ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d')) ax.fmt_xdata = DateFormatter('%Y-%m-%d') fig.autofmt_xdate() # add a legend ax.legend(['highest', 'closing', 'lowest'], loc='upper left') plt.show()
根据银行对账单的内容,这将生成 如下图所示:
rbcz.png
待办事项
- 覆盖率达到100%
- 决定是否分析IMAP语句时出错 例外情况
- 检查是否有可能改进解析-有很多 我扔的正则表达式不好看…
- 检查我认识的人是否得到捷克语语句,看看我们是否可以解析 他们也是。还有其他语言-德语吗?
- 检查是否适用于非捷克共和国的Raiffeisen