Advertisement
Guest User

Untitled

a guest
Feb 4th, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 KB | None | 0 0
  1. import requests
  2. import json
  3.  
  4.  
  5. def extract_time(json):
  6.     try:
  7.         return int(json[1]["updated"])
  8.     except KeyError:
  9.         return 0
  10.  
  11.  
  12. def main():
  13.     response = requests.get('https://api.exmo.com/v1/ticker/')
  14.  
  15.     json_data = json.loads(response.content.decode('utf-8'))
  16.  
  17.     json2 = []
  18.     for s in json_data.items():
  19.         json2.append(s)
  20.  
  21.     json2.sort(key=extract_time, reverse=True)
  22.  
  23.     fieldnames = ["symbol", "buy_price", "sell_price", "last_trade",
  24.                   "high", "low", "avg", "vol", "vol_curr", "updated"]
  25.  
  26.     with open('json_data.csv', "w") as out_file:
  27.         out_file.write(';'.join(fieldnames) + '\n')
  28.         for s in json2:
  29.             s1 = s[0] + ';'
  30.             s2 = ';'.join(map(str, s[1].values()))
  31.             out_file.write(s1 + s2 + '\n')
  32.  
  33.  
  34. if __name__ == '__main__':
  35.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement