我想在检查符号为BTC后访问价格低于USD的价格

时间:2019-07-07 11:45:00

标签: python json

我正在尝试访问嵌套字典,该字典在中间包含一个列表

r={
    "status": {
        "timestamp": "2019-07-07T09:26:17.112Z",
        "error_code": 0,
        "error_message": null,
        "elapsed": 11,
        "credit_count": 1
    },
    "data": [
        {
            "id": 1,
            "name": "Bitcoin",
            "symbol": "BTC",
            "slug": "bitcoin",
            "circulating_supply": 17803762,
            "total_supply": 17803762,
            "max_supply": 21000000,
            "date_added": "2013-04-28T00:00:00.000Z",
            "num_market_pairs": 7658,
            "tags": [
                "mineable"
            ],
            "platform": null,
            "cmc_rank": 1,
            "last_updated": "2019-07-07T09:25:27.000Z",
            "quote": {
                "USD": {
                    "price": 11209.5138577,
                    "volume_24h": 18924903576.9248,
                    "percent_change_1h": 0.12998,
                    "percent_change_24h": -1.4482,
                    "percent_change_7d": -4.43763,
                    "market_cap": 199571516858.19266,
                    "last_updated": "2019-07-07T09:25:27.000Z"
                }
            }
        },
 {
            "id": 1027,
            "name": "Ethereum",
            "symbol": "ETH",
            "slug": "ethereum",
            "circulating_supply": 106797544.1866,
            "total_supply": 106797544.1866,
            "max_supply": null,
            "date_added": "2015-08-07T00:00:00.000Z",
            "num_market_pairs": 5422,
            "tags": [
                "mineable"
            ],
            "platform": null,
            "cmc_rank": 2,
            "last_updated": "2019-07-07T09:25:19.000Z",
            "quote": {
                "USD": {
                    "price": 287.981264275,
                    "volume_24h": 7137227743.75008,
                    "percent_change_1h": -0.10015,
                    "percent_change_24h": -1.14435,
                    "percent_change_7d": -6.76952,
                    "market_cap": 30755691796.322243,
                    "last_updated": "2019-07-07T09:25:19.000Z"
                }
            }
        },

我在检查符号为BTC后试图在USD词典中访问价格

1 个答案:

答案 0 :(得分:0)

Python不支持null(例如"platform": null,),它应该放在双引号内或尝试使用None

for row in r['data']:
    if row['symbol'] in 'BTC':
        print(row['quote']['USD']['price'])

O / P:

11209.5138577
..