Serializing data to JSON in python -
can give me guidelines how serialize data file similar presented below?
{ "minshift": -0.5, "maxshift": 0.5, "stepshift": 0.002, "feeds": { "cfh": "cfh_20140318t0900.txt", "lmax": "lmax_20140318t0900.txt", "saxo": "saxo_20140318t0900.txt" }, "instruments": [ { "instrument_old": "cfh/eurusd", "instrument_new": "lmax/eurusd" }, { "instrument_old": "cfh/eurusd", "instrument_new": "saxo/eurusd" }, { "instrument_old": "lmax/xauusd", "instrument_new": "saxo/xauusd" } ] }
i have:
- shiftdata list 3 elements (for min, max , stepshift)
- dictionary key:value => {provider_name : filename}
- list of 2 element lists [instrument_new, instrument_old] named instrumentpairlist
i don't want nobody solve task, various tutorials on json in python rather blurred or simple in case:
- http://pymotw.com/2/json/
- https://python.readthedocs.org/en/v2.7.2/library/json.html
- http://freepythontips.wordpress.com/2013/08/08/storing-and-loading-data-with-json/
- python dump dict json file
- http://www.anthonydebarros.com/2012/03/11/generate-json-from-sql-using-python/
it trying serializing minshift, maxshift, stepshift , feeds:
data = { "minshift":shiftdata[0],"maxshift":shiftdata[1],"stepshift":shiftdata[2], "feeds":[ {key: value} key, value in provideraliases.items() ] } data_string = json.dumps(data)
my solution is:
data = { "minshift":shiftdata[0],"maxshift":shiftdata[1],"stepshift":shiftdata[2], "feeds":provideraliases, "instruments":[ {"instrument_old":pair[0],"instrument_new":pair[1]} pair in instrumentpairlist ]} data_string = json.dumps(data, indent=4) file = open(dir+r"\config.json","w") file.write(data_string) file.close()
Comments
Post a Comment