如何制作货币搜索过滤器?

塞芬

当使用 w = {"q": "25"} 时,它返回整个货币表,应该只发出美元汇率

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}")
阿南德·索米蒂兰

所以,只是为了过滤特定的国家代码,你可以在json返回后过滤它。


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}")
        

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章