Python - TypeError:不可用类型:'sl​​ice'错误

时间:2018-03-04 21:59:39

标签: python json string typeerror

我正在尝试使用以下代码访问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:不可用类型:'sl​​ice'错误

1 个答案:

答案 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'])