简体   繁体   中英

Python - TypeError: unhashable type: 'slice' error

I'm trying to access an API with this code:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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