As you can see the question itself is convoluted, but so is this problem. I'm trying to get the key/value pairs for name: name, symbol: symbol, and price: price in this API output. I've tried several things but can't seem to get it straight. Any help is appreciated.
Here is the dict:
data = {
'coins':
[
{
'id': 'bitcoin',
'icon': 'https://static.coinstats.app/coins/Bitcoin6l39t.png',
'name': 'Bitcoin',
'symbol': 'BTC',
'rank': 1,
'price': 48918.27974434776,
'priceBtc': 1,
'volume': 44805486777.77573,
'marketCap': 924655133704.012,
'availableSupply': 18902037,
'totalSupply': 21000000,
'priceChange1h': 0.05,
'priceChange1d': 1.22,
'priceChange1w': -3.02,
'websiteUrl': 'http://www.bitcoin.org',
'twitterUrl': 'https://twitter.com/bitcoin',
'exp': [
'https://blockchair.com/bitcoin/',
'https://btc.com/', 'https://btc.tokenview.com/'
]
},
{
'id': 'ethereum',
'icon': 'https://static.coinstats.app/coins/EthereumOCjgD.png',
'name': 'Ethereum',
'symbol': 'ETH',
'rank': 2,
'price': 4037.7735178501143,
'priceBtc': 0.08254618952631135,
'volume': 37993691263.19707,
'marketCap': 479507778421.57574,
'availableSupply': 118755491.4365,
'totalSupply': 0,
'priceChange1h': 0.3,
'priceChange1d': 4.53,
'priceChange1w': -8.85,
'websiteUrl': 'https://www.ethereum.org/',
'twitterUrl': 'https://twitter.com/ethereum',
'contractAddress': '0x2170ed0880ac9a755fd29b2688956bd959f933f8',
'decimals': 18,
'exp': [
'https://etherscan.io/',
'https://ethplorer.io/',
'https://blockchair.com/ethereum',
'https://eth.tokenview.com/'
]
}
]
}
Here's one of my failed attempts:
print(data['coins'][1])
new_dict = data['coins'][1]
for i in new_dict:
a = i.split(',')
print(a)
CodePudding user response:
I'm not sure what the expected output is since you do not clarify, though you can access each key-value for each stock as such:
for stock in data["coins"]:
print(stock["name"] " - " stock["symbol"] " - " str(stock["price"]))
Output:
Bitcoin - BTC - 48918.27974434776
Ethereum - ETH - 4037.7735178501143