Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For Lopop Python3
#1
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}
Reply
#2
Instead of
for key in sorted(obj):
    print("ASSET:",key, ':', obj[key])
Try:
for currency in sorted(obj):
    print(f"ASSET: {currency} {obj[currency]['usd']}")
Reply
#3
You can also use this
r = requests.get(url).text
obj = json.loads(r)
 
for key in (obj):
    print(f"ASSET:{key}. 'Value in USD: {obj[key].get('usd')}")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Gnuradio python3 is not compatible python3 xmlrpc library How Can I Fix İt ? muratoznnnn 3 4,823 Nov-07-2019, 05:47 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020