Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import json
- import re
- def parse_balances(path_to_logs):
- list_of_balances_dicts = []
- count_of_decode_errors = 0
- with open(path_to_logs, 'r') as logs:
- rows = logs.readlines()
- for row in rows:
- if row.find('balance') != -1:
- try:
- list_of_balances_dicts.append(json.loads(row[row.find('balance') + len('balance: '):]))
- except json.JSONDecodeError:
- count_of_decode_errors += 1
- return (list_of_balances_dicts, count_of_decode_errors)
- def create_balances_csv(path_to_logs, name='mm_balances.csv'):
- balances, errors_count = parse_balances(path_to_logs)
- print(f'balances rows = {len(balances)}; errors count = {errors_count}')
- df = pd.DataFrame(balances)
- df['update_time'] = pd.to_datetime(df['update_time'] * 1e6).dt.round('1s')
- df = df.drop_duplicates(subset=['asset', 'update_time']).sort_values('update_time')
- df.to_csv(name, index=False)
- return df
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement