i looped through a list returned by an API and grabbed 1.longName 2. RegularPrice 3.MarketCap which i have done but i want the market cap to return 'billion dollars' if its greater or equals 10 figures also return million dollars if it is less than 10 figures.
import requests
import pprint
import json
url = "https://yahoo-finance15.p.rapidapi.com/api/yahoo/ga/topgainers"
querystring = {"start":"0"}
headers = {
'x-rapidapi-host': "yahoo-finance15.p.rapidapi.com",
'x-rapidapi-key': "9efd0f3e52mshd859f5daf34a429p11cb2ajsn2b0e421d681e"
}
response = requests.request("GET", url, headers=headers, params=querystring)
data = response.json()
#print(response.text)
def new_stock(data):
new_market = []
for item in data ['quotes']:
new_name = item.get ('longName')
new_price = item.get ('regularMarketPrice')
res_price = (f'{new_price} Dollars')
cap =item.get('marketCap')
cap2 = str(cap)
for i in cap2:
if i == 1000000000:
return(f'{cap2} Billion Dollars')
else:
return(f'{cap2} million Dollars')
return cap2
new_market.append((new_name, res_price, cap2))
return new_market
value = new_stock(data)
dict = {i: value[i] for i in range(0, len(value))}
print(dict)
so i want a dictionary like
{Wienerberger AG', '4.38 Dollars', '2607606784 Billion Dollars} or {EPR Properties', '31.62 Dollars', '246636006 million Dollars}
Aucun commentaire:
Enregistrer un commentaire