简体   繁体   English

Python - 类型错误:不可散列类型:“切片”错误

[英]Python - TypeError: unhashable type: 'slice' error

I'm trying to access an API with this code:我正在尝试使用以下代码访问 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)

When I put [:4] in for x in date['valores'][:4]: , I get the following error:当我将[:4]放入for x in date['valores'][:4]: ,出现以下错误:

TypeError: unhashable type: 'slice' error类型错误:不可散列类型:“切片”错误

I'm gonna take a guess as to this is what you want:我会猜测这是你想要的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM