Created
April 4, 2019 08:33
-
-
Save muromec/f1840003a25162e47e100fb3151d6411 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from datetime import date | |
import json | |
import re | |
from collections import namedtuple | |
def make_date(date_str): | |
[day, month, year] = date_str.split('.') | |
return date(day=int(day), month=int(month), year=int(year)) | |
def make_num(num_str): | |
num = float(num_str.replace(' ', '')) | |
return int(num) if int(num) == num else num | |
Record = namedtuple('Record', [ | |
'edrpou', | |
'mfo', | |
'account', | |
'currency', | |
'docn', | |
'date', | |
'corr_mfo', | |
'corr_bank', | |
'corr_account', | |
'corr_edrpou', | |
'corr_name', | |
'sum', | |
'desc', | |
'x' | |
]) | |
def extract(): | |
with open('2019-usd.csv') as ffile: | |
next(ffile) | |
for line in ffile: | |
record = Record._make(line.strip().split(';')) | |
if 'BCD' in record.desc: | |
yield {"date": record.date, "sum": make_num(record.sum), "currency": record.currency} | |
with open('2019-uah.csv') as ffile: | |
next(ffile) | |
for line in ffile: | |
record = Record._make(line.strip().split(';')) | |
exchange_match = re.search(r'ГРИВНI ВIД ВIЛЬНОГО ПРОДАЖУ (?P<sum>\d+\.\d{0,2}) (?P<currency>[A-Z]{3}) ПО КУРСУ (?P<rate>\d{2}\.\d{0,2})', record.desc) | |
if exchange_match: | |
yield { | |
"date": record.date, | |
"sum": make_num(exchange_match.group('sum')), | |
"currency": exchange_match.group('currency'), | |
"rate": make_num(exchange_match.group('rate')), | |
"type": "exchange", | |
} | |
if record.desc.startswith('ГРИВНI ВIД ОБОВ`ЯЗКОВОГО ПРОДАЖУ '): | |
yield { | |
"date": record.date, | |
"sum": make_num(record.sum), | |
"currency": record.currency, | |
} | |
def main(): | |
print(json.dumps( | |
sorted(extract(), key=lambda x: make_date(x['date'])) | |
).replace('},', '},\n ') | |
.replace('}]', '}\n]') | |
) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment