我正在嘗試撰寫一個程式來讀取 JSON 檔案并從中獲取我需要的資訊。在我的 JSON 檔案中有一個引數:'exchangeId'它輸出交換號。我想確保只有我設定的那些交換,或者更確切地說是他們的'exchangeId',記錄在我的字典中,如果'exchangeId'串列中沒有硬幣,則該硬幣不想保存為 JSON 檔案。我怎樣才能做到這一點?
我的代碼:
response = s.get(url=url, headers=headers).json()
json_data = response['data']
pairs = []
out_object = {}
for pair in json_data["marketPairs"]:
pairs.append({
"exchange_name": pair["exchangeName"],
"market_url": pair["marketUrl"],
"price": pair["price"],
"last_update" : pair["lastUpdated"],
"exchange_id": pair["exchangeId"] # That ids what I'm would to have
# 102,311,200,302,521,433,482,406,42,400
})
out_object["name_of_coin"] = json_data["name"]
out_object["marketPairs"] = pairs
out_object["pairs"] = json_data["numMarketPairs"]
JSON 檔案:
{
"name_of_coin": "yearn.finance",
"marketPairs": [
{
"exchange_name": "Binance",
"market_url": "https://www.binance.com/en/trade/YFI_USDT",
"price": 24087.947333328204,
"last_update": "2021-12-07T17:49:54.000Z",
"exchange_id": 270
},
{
"exchange_name": "FTX",
"market_url": "https://ftx.com/trade/YFI/USD",
"price": 24110.0,
"last_update": "2021-12-07T17:49:53.000Z",
"exchange_id": 524
},
{
"exchange_name": "Coinbase Exchange",
"market_url": "https://pro.coinbase.com/trade/YFI-USD",
"price": 24085.37,
"last_update": "2021-12-07T17:48:57.000Z",
"exchange_id": 89
},
{
"exchange_name": "Huobi Global",
"market_url": "https://www.huobi.com/en-us/exchange/yfi_usdt",
"price": 24096.582498139305,
"last_update": "2021-12-07T17:49:53.000Z",
"exchange_id": 102
},
{
"exchange_name": "Bybit",
"market_url": "https://www.bybit.com/en-US/trade/spot/YFI/USDT",
"price": 24045.551976080656,
"last_update": "2021-12-07T17:48:10.000Z",
"exchange_id": 521
},
{
"exchange_name": "FTX",
"market_url": "https://ftx.com/trade/YFI/USDT",
"price": 24079.402222379562,
"last_update": "2021-12-07T17:49:53.000Z",
"exchange_id": 524
},
{
"exchange_name": "Binance",
"market_url": "https://www.binance.com/en/trade/YFI_BTC",
"price": 24090.344540422568,
"last_update": "2021-12-07T17:49:54.000Z",
"exchange_id": 270
},
{
"exchange_name": "Bithumb",
"market_url": "https://www.bithumb.com/trade/order/YFI_KRW",
"price": 25285.64198822739,
"last_update": "2021-12-07T17:49:53.000Z",
"exchange_id": 200
},
{
"exchange_name": "Gemini",
"market_url": "https://gemini.com/",
"price": 24088.85,
"last_update": "2021-12-07T17:49:55.000Z",
"exchange_id": 151
},
{
"exchange_name": "Bitfinex",
"market_url": "https://www.bitfinex.com/t/YFI:USD",
"price": 24117.0,
"last_update": "2021-12-07T17:49:54.000Z",
"exchange_id": 37
},
{
"exchange_name": "Binance",
"market_url": "https://www.binance.com/en/trade/YFI_BUSD",
"price": 24061.58763433993,
"last_update": "2021-12-07T17:49:54.000Z",
"exchange_id": 270
},
{
"exchange_name": "Coinbase Exchange",
"market_url": "https://pro.coinbase.com/trade/YFI-BTC",
"price": 24020.347144833217,
"last_update": "2021-12-07T17:48:57.000Z",
"exchange_id": 89
},
{
"exchange_name": "FTX US",
"market_url": "https://ftx.us/trade/YFI/USD",
"price": 23985.0,
"last_update": "2021-12-07T17:48:07.000Z",
"exchange_id": 1177
},
{
"exchange_name": "Binance",
"market_url": "https://www.binance.com/en/trade/YFI_EUR",
"price": 24018.992856877743,
"last_update": "2021-12-07T17:49:54.000Z",
"exchange_id": 270
},
{
"exchange_name": "Bitfinex",
"market_url": "https://www.bitfinex.com/t/YFI:UST",
"price": 24036.37648809482,
"last_update": "2021-12-07T17:49:54.000Z",
"exchange_id": 37
}
}
]
uj5u.com熱心網友回復:
要僅獲取您想要的交易所 ID 的資訊,只需在創建pairs串列時檢查它們。由于 id 是可散列的,因此在 Python 中最快的方法是將 id 存盤在 a 中set,這使得檢查成員資格非常快。
pairs = []
out_object = {}
desired_ids = {102,311,200,302,521,433,482,406,42,400} # Exchange ids
for pair in json_data["marketPairs"]:
if pair["exchangeId"] in desired_ids:
pairs.append({
"exchange_name": pair["exchangeName"],
"market_url": pair["marketUrl"],
"price": pair["price"],
"last_update" : pair["lastUpdated"],
"exchange_id": pair["exchangeId"]
})
uj5u.com熱心網友回復:
exchanges = [102,311,200,302,521,433,482,406,42,400]
for pair in json_data["marketPairs"]:
#Condition like Ronald Das Mentioned
if pair["exchange"] not in wantedExchanges:
pairs.append({
"exchange_name": pair["exchangeName"],
"market_url": pair["marketUrl"],
"price": pair["price"],
"last_update" : pair["lastUpdated"],
"exchange_id": pair["exchangeId"] #That ids what I'm would to have 102,311,200,302,521,433,482,406,42,400
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/375597.html
上一篇:Python串列到字典中
