简体   繁体   English

如何为 JSON 文件设置条件?

[英]How can I set a condition for a JSON file?

I'm trying to write a program that will read a JSON file and take the information I need from it.我正在尝试编写一个程序来读取 JSON 文件并从中获取我需要的信息。 In my JSON file there is a parameter: 'exchangeId' which outputs the exchange number.在我的 JSON 文件中有一个参数: 'exchangeId' ,它输出交换号。 I would like to make sure that only those exchanges that I set, or rather their 'exchangeId' , are recorded in my dictionary, and if coin don't have any 'exchangeId' at the list this coin don't wanna save as JSON file.我想确保只有我设置的那些交易所,或者更确切地说是他们'exchangeId' ,记录在我的字典中,如果硬币在列表中没有任何'exchangeId' ,这个硬币不想保存为 JSON文件。 How can I do this?我怎样才能做到这一点?

My code:我的代码:

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 file: 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
        }
     }
]
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
                                
             })

To only get information for the exchanges ids you want, just check for them when creating the pairs list.要仅获取您想要的交易所 ID 的信息,只需在创建pairs列表时检查它们。 Since the ids are hashable, the fastest way to do that in Python is by storing the ids in a set which makes checking for membership extremely fast.由于 id 是可散列的,因此在 Python 中最快的方法是将 id 存储在一个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"]
            })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM