简体   繁体   English

TypeError:列表索引必须是整数,而不是Python中的str

[英]TypeError: list indices must be integers, not str in Python

I have been having this problem for the last 2 days and i can't fix it. 我在过去的2天中一直遇到这个问题,但无法解决。

import urllib.request
import json
import base64
name = "koalaaaa"
url = "https://api.mojang.com/users/profiles/minecraft/" + name
rawdata = urllib.request.urlopen(url)
newrawdata = rawdata.read()
jsondata = json.loads(newrawdata.decode('utf-8'))
results = jsondata['id']
url_uuid = "https://api.mojang.com/user/profiles/" + results + "/names"
rawdata_uuid = urllib.request.urlopen(url_uuid)
newrawdata_uuid = rawdata_uuid.read()
jsondata_uuid = json.loads(newrawdata_uuid.decode('utf-8'))
results = jsondata_uuid['name']
print (results)

and i get the error: 我得到错误:

Traceback (most recent call last):
File "C:/Python34/unmigrated.py", line 14, in <module>
results = jsondata_uuid['name']
TypeError: list indices must be integers, not str

jsondata_uuid is a list of dicts, each of which contains a name . jsondata_uuid是字典的列表,每个字典都包含一个name To print them all out you can do print([data['name'] for data in jsondata_uuid]) . 要全部打印出来,可以执行print([data['name'] for data in jsondata_uuid])

Tip: Use requests instead of urllib . 提示:使用请求代替urllib

Your jsondata_uuid contains a list of dictionaries with something like this: 您的jsondata_uuid包含具有以下内容的字典列表:

[{'name': 'Deamti'}, {'changedToAt': 1423576506000, 'name': 'ChocolateBanana'}, {'changedToAt': 1426793965000, 'name': 'IMBHARLOW'}, {'changedToAt': 1430137396000, 'name': 'SuckUp'}, {'changedToAt': 1437970183000, 'name': 'lllllll'}, {'changedToAt': 1440592843000, 'name': 'Koalaaaa'}]

Which 'name' key you want? 您想要哪个“名称”键?

Change the line results = jsondata_uuid['name'] for results = jsondata_uuid and you will see the results. 更改行results = jsondata_uuid['name']results = jsondata_uuid ,你会看到结果。 Then, you can think of a way of sorting the names out. 然后,您可以想到一种对名称进行排序的方法。

jsondata_uuid is a list you cannot use a key to fetch items from it instead you can use numbers. jsondata_uuid是一个列表,您不能使用键从中获取项目,而可以使用数字。

if it contains a dictionary as the first item you can fetch a value from the dictionary using this for example : 如果它包含字典作为第一项,则可以使用以下命令从字典中获取值:

results = jsondata_uuid[0]['name']

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

相关问题 &#39;TypeError:列表索引必须是整数,而不是str&#39;Python 3 - 'TypeError: list indices must be integers, not str' Python 3 类型错误:列表索引必须是整数,而不是 str - python - TypeError: list indices must be integers, not str - python TypeError:list indices必须是整数,而不是str Python - TypeError: list indices must be integers, not str Python Python 类型错误:列表索引必须是整数,而不是 str - Python TypeError: list indices must be integers, not str Python:TypeError:list indices必须是整数,而不是str - Python: TypeError: list indices must be integers, not str Python-&gt; TypeError:列表索引必须是整数,而不是str - Python -> TypeError: list indices must be integers, not str Python列表循环错误:TypeError:列表索引必须是整数,而不是str - Python List Loop Error: TypeError: list indices must be integers, not str Python3-TypeError:列表索引必须是整数或切片,而不是str-List - Python3 - TypeError: list indices must be integers or slices, not str - List 使用Python解析JSON:TypeError:list indices必须是整数,而不是str - Parsing JSON with Python: TypeError: list indices must be integers, not str Python chatbot-TypeError:列表索引必须是整数,而不是str - Python chatbot - TypeError: list indices must be integers, not str
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM