An error is issued when running the code " txt = data['txt'], int("str") TypeError: list indices must be integers or slices, not str " `
import requests
import json
ua = "https://bank.gov.ua/NBUStatService/v1/statdirectory/exchange?json"
ubank = requests.get(ua)
if ubank.status_code == 200:
data = json.loads(ubank.text)
txt = data['txt']
cc = data['cc']
rate = data['rate']
else:
print("error")
print(" Валюта :{txt} \n код :{cc} \n курс :{rate}")
`
CodePudding user response:
If you're trying to print all the available rates in data
, modify your code to the below:
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} \nкод :{cc} \nкурс :{rate}")
else:
print("error")