[英]Keyerror in Python when retrieving json data
我創建了一個 Python 腳本來從加密貨幣交易所獲取未結訂單。
當我使用以下命令連接到 api 時:
order = exchange.fetch_open_orders(symbol)
我收到以下回復:
[{'info': {'symbol': 'ETHBTC',
'orderId': 507325551, 'orderListId': -1, 'clientOrderId':
'web_b75c7f9be90849beac14cd86f575ac01', 'price': '0.02504200',
'origQty': '0.02100000', 'executedQty': '0.00000000',
'cummulativeQuoteQty': '0.00000000', 'status': 'NEW',
'timeInForce': 'GTC', 'type': 'LIMIT', 'side': 'SELL',
'stopPrice': '0.00000000', 'icebergQty': '0.00000000',
'time': 1571163346981, 'updateTime': 1571163346981, 'isWorking': True},
'id': '507325551', 'timestamp': 1571163346981, 'datetime': '2019-10-15T18:15:46.981Z',
'lastTradeTimestamp': None,
'symbol': 'ETH/BTC', 'type': 'limit',
'side': 'sell', 'price': 0.025042,
'amount': 0.021, 'cost': 0.0, 'average': None, 'filled': 0.0,
'remaining': 0.021, 'status': 'open', 'fee': None, 'trades': None}]
這是一個很長的響應,我不想要這里的所有內容,為了只打印一些值,我嘗試了這個:
for x in order:
sym = x['symbol']
price = x['price']
status = x['status']
amount = x['amount']
side = x['side']
orig = x['origQty']
print(sym, price, status, amount, side, orig)
此代碼一直有效,直到我得到KeyError: 'origQty'
的行orig = x['origQty']
。
我不明白這是從哪里來的,因為所有其他變量的打印都沒有任何錯誤,並且因為origQty
在響應中,而當我嘗試查找不存在的東西時,通常會出現此錯誤。 有人可以幫我找出我做錯了什么嗎?
'origQty'
嵌套在'info'
鍵下,嘗試使用x['info']['origQty']
代替。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.