Advertisement
den4ik2003

Untitled

Apr 4th, 2024
833
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1. import pandas as pd
  2. import json
  3. import re
  4.  
  5.  
  6. def parse_balances(path_to_logs):
  7.     list_of_balances_dicts = []
  8.     count_of_decode_errors = 0
  9.    
  10.     with open(path_to_logs, 'r') as logs:
  11.         rows = logs.readlines()
  12.         for row in rows:
  13.             if row.find('balance') != -1:
  14.                 try:
  15.                     list_of_balances_dicts.append(json.loads(row[row.find('balance') + len('balance: '):]))
  16.                 except json.JSONDecodeError:
  17.                     count_of_decode_errors += 1
  18.    
  19.     return (list_of_balances_dicts, count_of_decode_errors)
  20.  
  21.  
  22. def create_balances_csv(path_to_logs, name='mm_balances.csv'):
  23.     balances, errors_count = parse_balances(path_to_logs)
  24.     print(f'balances rows = {len(balances)}; errors count = {errors_count}')
  25.    
  26.     df = pd.DataFrame(balances)
  27.     df['update_time'] = pd.to_datetime(df['update_time'] * 1e6).dt.round('1s')
  28.     df = df.drop_duplicates(subset=['asset', 'update_time']).sort_values('update_time')
  29.     df.to_csv(name, index=False)
  30.  
  31.     return df
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement