我正在尝试使用以下代码访问API:
import requests
import json
req = requests.get('http://api.promasters.net.br/cotacao/v1/valores')
date = json.loads(req.text)
data = req.json()
for x in date['valores'][:4]:
coin = x['moeda']
print(coin)
当我将[:4]
放入for x in date['valores'][:4]:
时,我收到以下错误:
TypeError:不可用类型:'slice'错误
答案 0 :(得分:0)
我会猜测这是你想要的:
import requests
import json
req = requests.get('http://api.promasters.net.br/cotacao/v1/valores')
date = json.loads(req.text)
data = req.json()
for x in list(date['valores'].items())[:4]:
print(x[0], x[1]['valor'])