[英]Serializing data to JSON in python
有人能給我一些指導如何將數據序列化到類似於下面介紹的文件嗎?
{
"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"
}
]
}
我有:
我不希望沒有人來解決我的任務,但是在我的情況下,Python 中有關 JSON 的各種教程相當模糊或簡單:
它正在嘗試序列化 minShift、maxShift、stepShift 和 feeds:
data = { "minShift":shiftData[0],"maxShift":shiftData[1],"stepShift":shiftData[2],
"feeds":[ {key: value} for key, value in providerAliases.items() ] }
data_string = json.dumps(data)
我的解決辦法是:
data = { "minShift":shiftData[0],"maxShift":shiftData[1],"stepShift":shiftData[2],
"feeds":providerAliases,
"instruments":[ {"instrument_old":pair[0],"instrument_new":pair[1]}
for pair in instrumentPairList ]}
data_string = json.dumps(data, indent=4)
file = open(dir+r"\config.json","w")
file.write(data_string)
file.close()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.