我是python的新手,我成功地提取了'symbol'和'ask'值,但在json中,'ask'有2个值,我只想在逗号之前获得第一部分。我试着在论坛上找到答案,但它从来没有匹配我的问题。
这是我的代码
exchange2 = requests.get("https://ascendex.com/api/pro/v2/futures/ticker")
e = exchange2.json()
exchange2 = json_normalize(e['data'])
exchange2['symbol'] = exchange2['symbol'].str.replace('-PERP', 'USDT')
exchange2 = pd.DataFrame(exchange2, columns=['symbol', 'ask'])
print (exchange2)
结果是
symbol ask
0 SHIBUSDT [0.00001077, 3996990]
1 VETUSDT [0.03033, 953000]
我的预期结果是这个
symbol ask
0 SHIBUSDT 0.00001077
1 VETUSDT 0.03033
需要的数据值为json data的ask key
中的list。因此,您可以使用list slicing
方法
exchange2 = requests.get("https://ascendex.com/api/pro/v2/futures/ticker")
e = exchange2.json()
# k=json.dumps(e)
# #print(e)
# with open('aj.json', 'w') as f:
# f.write(k)
exchange=[]
for item in e['data']:
symbol= item['symbol'].replace('-PERP', 'USDT')
ask=item['ask'][0]
print(ask)
exchange.append({
'symbol':symbol,
'ask':ask})
df = pd.DataFrame(exchange)
print (df)
输出:
symbol ask
0 SHIBUSDT 0.00001079
1 VETUSDT 0.03033
2 ALGOUSDT 0.3895
3 FTTUSDT 25.83
4 TRXUSDT 0.08098
5 ADAUSDT 0.558
6 FIDAUSDT 0.4855
7 ETHUSDT 1788.8
8 PORTUSDT 999999999
9 BCHUSDT 187.5
10 ATOMUSDT 9.21
11 XTZUSDT 1.913
12 XPRTUSDT 1.408
13 BNBUSDT 297.3
14 LTCUSDT 62.87
15 SOLUSDT 39.463
16 UNIUSDT 4.98
17 ZILUSDT 0.04978
18 IOSTUSDT 0.0173
19 DOGEUSDT 0.08159
20 EOSUSDT 1.271
21 EGLDUSDT 75.9
22 BTCUSDT 29718
23 BCHSVUSDT 56.53
24 APEUSDT 6.115
25 OMIUSDT 0.001624
26 DOTUSDT 9.33
27 MATICUSDT 0.589
28 AKTUSDT 0.3872
29 XRPUSDT 0.3941
30 FILUSDT 7.35
31 CSPRUSDT 0.036
32 ETCUSDT 21.82
33 WOOUSDT 0.1585
34 LINKUSDT 7.327
35 SRMUSDT 1.035
36 FTMUSDT 0.3484
37 ONEUSDT 0.04104
38 XLMUSDT 0.14468