Created
August 17, 2017 20:12
-
-
Save inesusvet/e2dee03e7b4fe11bfcb8726f6410501f to your computer and use it in GitHub Desktop.
Преобразование выгрузки транзакций из "нативного" php формата в csv
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 __future__ import print_function | |
INPUT_FILENAME = 'export_dmr.txt' | |
OUTPUT_FILENAME = 'export_dmr.csv' | |
FIELDS = [ | |
'dmr_transaction_id', | |
'paid_at', | |
'purchase_price', | |
'purchase_currency', | |
'order_price', | |
'order_currency', | |
'order_rate', | |
'merchant', | |
] | |
def chunks(iterable, size): | |
while len(iterable): | |
yield iterable[:size] | |
iterable = iterable[size:] | |
def iter_lines(filename): | |
i = 0 | |
with open(filename) as input_file: | |
for line in input_file: | |
i += 1 | |
if i < 5: | |
continue | |
yield line.strip() | |
if i == 14: | |
i = 0 | |
def parse_row(lines, fields): | |
for line in lines: | |
name = line.split(']', 1)[0][1:] | |
if name not in fields: | |
continue | |
value = line.split('=> ')[1] | |
yield value | |
def main(filename, fields): | |
for chunk in chunks(list(iter_lines(filename)), 10): | |
values = list(parse_row(chunk, fields)) | |
yield values | |
if __name__ == '__main__': | |
with open(OUTPUT_FILENAME, 'w') as output_file: | |
header = '%s\n' % ','.join(FIELDS) | |
output_file.write(header) | |
for row in main(INPUT_FILENAME, FIELDS): | |
line = '%s\n' % ','.join(row) | |
output_file.write(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment