Error TypeError: list indices must be integers or slices, not str
Error TypeError: list indices must be integers or slices, not str
当 运行 代码 " txt = data['txt'], int("str") 时出现错误
类型错误:列表索引必须是整数或切片,而不是 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}")
`
如果您尝试打印 data
中的所有可用费率,请将您的代码修改为以下内容:
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")
当 运行 代码 " txt = data['txt'], int("str") 时出现错误 类型错误:列表索引必须是整数或切片,而不是 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}")
`
如果您尝试打印 data
中的所有可用费率,请将您的代码修改为以下内容:
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")