嗨,我正在尝试制作一个Json文件,从中我将获取正在构建的程序的数据。我想制作一个Json文件,其中将有一个名为symbol的主键,如下所示:>
{
"symbol": [
{
"name": "LTCUSDT",
"buy": "0.5",
"sell": "0.5"
},
{
"name": "BTCUSDT",
"buy": "0.1",
"sell": "0.1"
}
]
}
所有这些将由列表中存储的信息创建
import json
pairs = ['LTCUSDT', 'BTCUSDT']
buy = [0.5 ,0.1]
sell = [0.1 ,0.1]
答案 0 :(得分:1)
您可以使用pandas获取内部列表,然后将其放入json:
import pandas as pd
import json
pairs = ['LTCUSDT', 'BTCUSDT']
buy = [0.5 ,0.1]
sell = [0.1 ,0.1]
df = pd.DataFrame([pairs, buy, sell], index=['name', 'buy', 'sell'])
inner_list = [v for k,v in df.to_dict().items()]
final_dict = {'symbol':inner_list}
print(json.dumps(final_dict,indent=4))