Mar-02-2022, 01:56 AM
Quote:I am trying to obtain the asset and its value from the result without the curly braces, I have tried to do it through various ways and I have not succeeded, could someone help me please?
The result I want to get is ASSET: bitcoin 43935
from pycoingecko import CoinGeckoAPI import requests import json import urllib base_url = 'https://api.coingecko.com/api/v3/' url = base_url + '/coins' r = requests.get(url) identifiers = r.json() #print(identifiers) symbol_id_map = {d['symbol']:d['id'] for d in identifiers} #print(symbol_id_map) symbols = ['BTC', 'ETH','USDT' ] ids = [symbol_id_map[symbol.lower()] for symbol in symbols] ids = ",".join(ids) url = base_url + f'/simple/price?ids={ids}&vs_currencies=usd' r = requests.get(url) print(r.json()) print(type(r)) obj = r.json() print(type(obj)) print(obj) for key in sorted(obj): print("ASSET:",key, ':', obj[key])
Output:{'bitcoin': {'usd': 43935}, 'ethereum': {'usd': 2923.98}, 'tether': {'usd': 0.999972}}
<class 'requests.models.Response'>
<class 'dict'>
{'bitcoin': {'usd': 43935}, 'ethereum': {'usd': 2923.98}, 'tether': {'usd': 0.999972}}
ASSET: bitcoin : {'usd': 43935}
ASSET: ethereum : {'usd': 2923.98}
ASSET: tether : {'usd': 0.999972}