Possible to write ordered json data into file? How to do it using Python? -


by using http request, write exchange rates http://openexchangerates.org/api/ file, looks this:

{"timestamp": 1436266865, "rates": {"eur": 0.911228, "jpy": 122.5463, "aud": 1.346118}} {"rates": {"aud": 1.345790, "eur": 0.912256, "jpy": 122.4428}, "timestamp": 1436270462} {"timestamp": 1436277661, "rates": {"jpy": 122.4789, "aud": 1.348871, "eur": 0.914330}} 

is possible write file somehow data ordered?

  1. i'd "timestamp" precede "rates".
  2. i'd currencies ordered (either alphabetically using 3-character codes "aud", "eur", "jpy" or using own custom order, say, want "jpy","eur","aud").

with alphabetical ordering, desired result be:

{"timestamp": 1436266865, "rates": {"aud": 1.346118, "eur": 0.911228, "jpy": 122.5463}} {"timestamp": 1436270462, "rates": {"aud": 1.345790, "eur": 0.912256, "jpy": 122.4428}} {"timestamp": 1436277661, "rates": {"aud": 1.348871, "eur": 0.914330, "jpy": 122.4789}} 

my python script, fetches data is:

import json, requests  base_url = 'http://openexchangerates.org/api/' app_id = 'my_secret_app_id_here'  def fetch_fx():     uri = 'latest.json'     fx_all = (requests.get(base_url + uri + '?app_id=' + app_id)).json()     rates = {k:fx_all[k] k in ('timestamp', 'rates')}     open('/datafiles/fx.json', 'a') f:         f.writelines(json.dumps(rates) + '\n')  = fetch_fx() 

if want not possible due http-get or json design, achieve ordering using python's collections.ordereddict in script.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -