@defild

Как вывести все поля у которых название одно и тоже а значение другое?

Я делаю запрос на сайт, сайт выдает мне json значения и одно из полей оно повторяется много раз мне надо все эти поля вывести в нормальном виде вот пример моего кода:
r2 = requests.get(f"https://api.coingecko.com/api/v3/coins/{slug}/tickers")
        data2 = r2.json()
        pprint(data2)
market_name = []
        for i in range(data2):
            market_name[i] = data2['tickers'][i]['market']['name']
            print(f"Market name: {market_name[i]}")

Вот Json:
{'name': 'Bitcoin',
 'tickers': [{'base': 'BTC',
              'bid_ask_spread_percentage': 0.010034,
              'coin_id': 'bitcoin',
              'converted_last': {'btc': 1.000832,
                                 'eth': 14.633225,
                                 'usd': 29784},
              'converted_volume': {'btc': 48398,
                                   'eth': 707624,
                                   'usd': 1440259765},
              'is_anomaly': False,
              'is_stale': False,
              'last': 29668.22,
              'last_fetch_at': '2022-05-15T10:13:49+00:00',
              'last_traded_at': '2022-05-15T10:13:49+00:00',
              'market': {'has_trading_incentive': False,
                         'identifier': 'binance',
                         'name': 'Binance'},
              'target': 'USDT',
              'target_coin_id': 'tether',
              'timestamp': '2022-05-15T10:13:49+00:00',
              'token_info_url': None,
              'trade_url': 'https://www.binance.com/en/trade/BTC_USDT?ref=37754157',
              'trust_score': 'green',
              'volume': 48357.32095661529},
             {'base': 'WBTC',
              'bid_ask_spread_percentage': 0.010002,
              'coin_id': 'wrapped-bitcoin',
              'converted_last': {'btc': 1.0, 'eth': 14.603248, 'usd': 29671},
              'converted_volume': {'btc': 137.98, 'eth': 2015, 'usd': 4093977},
              'is_anomaly': False,
              'is_stale': False,
              'last': 0.9997,
              'last_fetch_at': '2022-05-15T10:23:54+00:00',
              'last_traded_at': '2022-05-15T10:19:38+00:00',
              'market': {'has_trading_incentive': False,
                         'identifier': 'ftx_spot',
                         'name': 'FTX'},
              'target': 'BTC',
              'target_coin_id': 'bitcoin',
              'timestamp': '2022-05-15T10:19:38+00:00',
              'token_info_url': None,
              'trade_url': 'https://ftx.com/trade/WBTC/BTC',
              'trust_score': 'green',
              'volume': 138.02190083024908},
             {'base': 'BTC',
              'bid_ask_spread_percentage': 0.016816,
              'coin_id': 'bitcoin',
              'converted_last': {'btc': 1.000886,
                                 'eth': 14.620339,
                                 'usd': 29721},
              'converted_volume': {'btc': 13166,
                                   'eth': 192320,
                                   'usd': 390963508},
              'is_anomaly': False,
              'is_stale': False,
              'last': 29652.24,
              'last_fetch_at': '2022-05-15T10:22:07+00:00',
              'last_traded_at': '2022-05-15T10:22:07+00:00',
              'market': {'has_trading_incentive': False,
                         'identifier': 'digifinex',
                         'name': 'Digifinex'},
              'target': 'USDT',
              'target_coin_id': 'tether',
              'timestamp': '2022-05-15T10:22:07+00:00',
              'token_info_url': None,
              'trade_url': 'https://www.digifinex.com/en-ww/trade/USDT/BTC',
              'trust_score': 'green',
              'volume': 13154.30795151},
             {'base': 'ETH',
              'bid_ask_spread_percentage': 0.011459,
              'coin_id': 'ethereum',
              'converted_last': {'btc': 1.0, 'eth': 14.603248, 'usd': 29671},
              'converted_volume': {'btc': 3337, 'eth': 48727, 'usd': 99002386},
              'is_anomaly': False,
              'is_stale': False,
              'last': 0.068503,
              'last_fetch_at': '2022-05-15T10:23:58+00:00',
              'last_traded_at': '2022-05-15T10:23:58+00:00',
              'market': {'has_trading_incentive': False,
                         'identifier': 'whitebit',
                         'name': 'WhiteBIT'},
              'target': 'BTC',
              'target_coin_id': 'bitcoin',
              'timestamp': '2022-05-15T10:23:58+00:00',
              'token_info_url': None,
              'trade_url': 'https://whitebit.com/trade/ETH_BTC',
              'trust_score': 'green',
              'volume': 48708.902},
             {'base': 'BTC',
              'bid_ask_spread_percentage': 0.01338,
              'coin_id': 'bitcoin',
              'converted_last': {'btc': 0.99724658,
                                 'eth': 14.563039,
                                 'usd': 29589},
              'converted_volume': {'btc': 2150, 'eth': 31403, 'usd': 63803915},
              'is_anomaly': False,
              'is_stale': False,
              'last': 29589.0,
              'last_fetch_at': '2022-05-15T10:23:57+00:00',
              'last_traded_at': '2022-05-15T10:23:57+00:00',
              'market': {'has_trading_incentive': False,
                         'identifier': 'ftx_us',
                         'name': 'FTX.US'},
              'target': 'USD',
              'timestamp': '2022-05-15T10:23:57+00:00',
              'token_info_url': None,
              'trade_url': 'https://ftx.us/trade/BTC/USD',
              'trust_score': 'green',
              'volume': 2156.3390012132886},
             {'base': 'BTC',
              'bid_ask_spread_percentage': 0.013498,
              'coin_id': 'bitcoin',
              'converted_last': {'btc': 0.99998279,
                                 'eth': 14.602997,
                                 'usd': 29670},
              'converted_volume': {'btc': 192.67, 'eth': 2814, 'usd': 5716642},
              'is_anomaly': False,
              'is_stale': False,
              'last': 29634.0,
              'last_fetch_at': '2022-05-15T10:23:58+00:00',
              'last_traded_at': '2022-05-15T10:23:58+00:00',
              'market': {'has_trading_incentive': False,
                         'identifier': 'ftx_us',
                         'name': 'FTX.US'},
              'target': 'USDT',
              'target_coin_id': 'tether',
              'timestamp': '2022-05-15T10:23:58+00:00',
              'token_info_url': None,
              'trade_url': 'https://ftx.us/trade/BTC/USDT',
              'trust_score': 'green',
              'volume': 192.672936569481},
             {'base': 'BTC',
              'bid_ask_spread_percentage': 0.033738,
              'coin_id': 'bitcoin',
              'converted_last': {'btc': 0.99687626,
                                 'eth': 14.579921,
                                 'usd': 29634},
              'converted_volume': {'btc': 3782, 'eth': 55319, 'usd': 112436637},
              'is_anomaly': False,
              'is_stale': False,
              'last': 29634.0638176,
              'last_fetch_at': '2022-05-15T10:18:21+00:00',
              'last_traded_at': '2022-05-15T10:18:21+00:00',
              'market': {'has_trading_incentive': False,
                         'identifier': 'bitfinex',
                         'name': 'Bitfinex'},
              'target': 'USD',
              'timestamp': '2022-05-15T10:18:21+00:00',
              'token_info_url': None,
              'trade_url': 'https://www.bitfinex.com/t/BTCUSD',
              'trust_score': 'green',
              'volume': 3794.16867832},

Мне надо забрать с json все market => name
  • Вопрос задан
  • 72 просмотра
Пригласить эксперта
Ответы на вопрос 1
@AVKor
вот пример моего кода

... совершенно нерабочего прежде всего из-за "левых" отступов. Но не только.
Вот Json:

Некорректный.

Ниже исправленный JSON и как из него получить то, что требуется.
import json

DOC = """\
{
  "name": "Bitcoin",
  "tickers": [
    {
      "base": "BTC",
      "bid_ask_spread_percentage": 0.010034,
      "coin_id": "bitcoin",
      "converted_last": {
        "btc": 1.000832,
        "eth": 14.633225,
        "usd": 29784
      },
      "converted_volume": {
        "btc": 48398,
        "eth": 707624,
        "usd": 1440259765
      },
      "is_anomaly": false,
      "is_stale": false,
      "last": 29668.22,
      "last_fetch_at": "2022-05-15T10:13:49+00:00",
      "last_traded_at": "2022-05-15T10:13:49+00:00",
      "market": {
        "has_trading_incentive": false,
        "identifier": "binance",
        "name": "Binance"
      },
      "target": "USDT",
      "target_coin_id": "tether",
      "timestamp": "2022-05-15T10:13:49+00:00",
      "token_info_url": null,
      "trade_url": "https://www.binance.com/en/trade/BTC_USDT?ref=37754157",
      "trust_score": "green",
      "volume": 48357.32095661529
    },
    {
      "base": "WBTC",
      "bid_ask_spread_percentage": 0.010002,
      "coin_id": "wrapped-bitcoin",
      "converted_last": {
        "btc": 1,
        "eth": 14.603248,
        "usd": 29671
      },
      "converted_volume": {
        "btc": 137.98,
        "eth": 2015,
        "usd": 4093977
      },
      "is_anomaly": false,
      "is_stale": false,
      "last": 0.9997,
      "last_fetch_at": "2022-05-15T10:23:54+00:00",
      "last_traded_at": "2022-05-15T10:19:38+00:00",
      "market": {
        "has_trading_incentive": false,
        "identifier": "ftx_spot",
        "name": "FTX"
      },
      "target": "BTC",
      "target_coin_id": "bitcoin",
      "timestamp": "2022-05-15T10:19:38+00:00",
      "token_info_url": null,
      "trade_url": "https://ftx.com/trade/WBTC/BTC",
      "trust_score": "green",
      "volume": 138.02190083024908
    },
    {
      "base": "BTC",
      "bid_ask_spread_percentage": 0.016816,
      "coin_id": "bitcoin",
      "converted_last": {
        "btc": 1.000886,
        "eth": 14.620339,
        "usd": 29721
      },
      "converted_volume": {
        "btc": 13166,
        "eth": 192320,
        "usd": 390963508
      },
      "is_anomaly": false,
      "is_stale": false,
      "last": 29652.24,
      "last_fetch_at": "2022-05-15T10:22:07+00:00",
      "last_traded_at": "2022-05-15T10:22:07+00:00",
      "market": {
        "has_trading_incentive": false,
        "identifier": "digifinex",
        "name": "Digifinex"
      },
      "target": "USDT",
      "target_coin_id": "tether",
      "timestamp": "2022-05-15T10:22:07+00:00",
      "token_info_url": null,
      "trade_url": "https://www.digifinex.com/en-ww/trade/USDT/BTC",
      "trust_score": "green",
      "volume": 13154.30795151
    },
    {
      "base": "ETH",
      "bid_ask_spread_percentage": 0.011459,
      "coin_id": "ethereum",
      "converted_last": {
        "btc": 1,
        "eth": 14.603248,
        "usd": 29671
      },
      "converted_volume": {
        "btc": 3337,
        "eth": 48727,
        "usd": 99002386
      },
      "is_anomaly": false,
      "is_stale": false,
      "last": 0.068503,
      "last_fetch_at": "2022-05-15T10:23:58+00:00",
      "last_traded_at": "2022-05-15T10:23:58+00:00",
      "market": {
        "has_trading_incentive": false,
        "identifier": "whitebit",
        "name": "WhiteBIT"
      },
      "target": "BTC",
      "target_coin_id": "bitcoin",
      "timestamp": "2022-05-15T10:23:58+00:00",
      "token_info_url": null,
      "trade_url": "https://whitebit.com/trade/ETH_BTC",
      "trust_score": "green",
      "volume": 48708.902
    },
    {
      "base": "BTC",
      "bid_ask_spread_percentage": 0.01338,
      "coin_id": "bitcoin",
      "converted_last": {
        "btc": 0.99724658,
        "eth": 14.563039,
        "usd": 29589
      },
      "converted_volume": {
        "btc": 2150,
        "eth": 31403,
        "usd": 63803915
      },
      "is_anomaly": false,
      "is_stale": false,
      "last": 29589,
      "last_fetch_at": "2022-05-15T10:23:57+00:00",
      "last_traded_at": "2022-05-15T10:23:57+00:00",
      "market": {
        "has_trading_incentive": false,
        "identifier": "ftx_us",
        "name": "FTX.US"
      },
      "target": "USD",
      "timestamp": "2022-05-15T10:23:57+00:00",
      "token_info_url": null,
      "trade_url": "https://ftx.us/trade/BTC/USD",
      "trust_score": "green",
      "volume": 2156.3390012132886
    },
    {
      "base": "BTC",
      "bid_ask_spread_percentage": 0.013498,
      "coin_id": "bitcoin",
      "converted_last": {
        "btc": 0.99998279,
        "eth": 14.602997,
        "usd": 29670
      },
      "converted_volume": {
        "btc": 192.67,
        "eth": 2814,
        "usd": 5716642
      },
      "is_anomaly": false,
      "is_stale": false,
      "last": 29634,
      "last_fetch_at": "2022-05-15T10:23:58+00:00",
      "last_traded_at": "2022-05-15T10:23:58+00:00",
      "market": {
        "has_trading_incentive": false,
        "identifier": "ftx_us",
        "name": "FTX.US"
      },
      "target": "USDT",
      "target_coin_id": "tether",
      "timestamp": "2022-05-15T10:23:58+00:00",
      "token_info_url": null,
      "trade_url": "https://ftx.us/trade/BTC/USDT",
      "trust_score": "green",
      "volume": 192.672936569481
    },
    {
      "base": "BTC",
      "bid_ask_spread_percentage": 0.033738,
      "coin_id": "bitcoin",
      "converted_last": {
        "btc": 0.99687626,
        "eth": 14.579921,
        "usd": 29634
      },
      "converted_volume": {
        "btc": 3782,
        "eth": 55319,
        "usd": 112436637
      },
      "is_anomaly": false,
      "is_stale": false,
      "last": 29634.0638176,
      "last_fetch_at": "2022-05-15T10:18:21+00:00",
      "last_traded_at": "2022-05-15T10:18:21+00:00",
      "market": {
        "has_trading_incentive": false,
        "identifier": "bitfinex",
        "name": "Bitfinex"
      },
      "target": "USD",
      "timestamp": "2022-05-15T10:18:21+00:00",
      "token_info_url": null,
      "trade_url": "https://www.bitfinex.com/t/BTCUSD",
      "trust_score": "green",
      "volume": 3794.16867832
    }
  ]
}\
"""
data = json.loads(DOC)
market_name = []
for ticket in data['tickers']:
    market_name.append(ticket['market']['name'])
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы
22 нояб. 2024, в 00:55
500 руб./за проект
21 нояб. 2024, в 23:30
300000 руб./за проект
21 нояб. 2024, в 22:21
3000 руб./в час