When w = {"q": "25"} is used, it returns the entire currency table, only the dollar exchange rate should be issued
import requests
import json
ua = "https://bank.gov.ua/NBUStatService/v1/statdirectory/exchange?json"
w = {"q": "25"}
ubank = requests.get(ua, w)
if ubank.status_code == 200:
data = json.loads(ubank.text)
for d in data:
txt = d['txt']
cc = d['cc']
rate = d['rate']
print(f"Валюта: {txt} код: {cc} курс: {rate}")
CodePudding user response:
So, just to filter by a particular country code, you can filter the json after it is returned.
ubank = requests.get(ua)
if ubank.status_code == 200:
data = json.loads(ubank.text)
#filter for the country/currency we require
#the below iterates each element in objects array
#data2 = [x for x in data if x['r030'] == 124]
#alterntively, can use `lambda` too to filter
data2 = filter(lambda x: x.get('r030') == 124, data)
for d in data2:
kv = d['r030']
txt = d['txt']
cc = d['cc']
rate = d['rate']
print(f"Валюта: {kv} {txt} код: {cc} курс: {rate}")