简体   繁体   English

类型错误:列表索引必须是整数,而不是 str - python

[英]TypeError: list indices must be integers, not str - python

I am attempting to access a JSON api and print only the "genres" but i keep getting the following error:我正在尝试访问 JSON api 并仅打印“流派”,但我不断收到以下错误:

TypeError: list indices must be integers, not str类型错误:列表索引必须是整数,而不是 str

My code:我的代码:

import urllib
import json

u = urllib.urlopen('http://api.tvmaze.com/search/shows?q=the%20big%20bang%20theory')
data = json.load(u)   


print data['genres']

if I just try:如果我只是尝试:

print data

I get all the information back.我取回了所有信息。

Thanks in advance for the help.在此先感谢您的帮助。

You should have a look at the return type of json.load .你应该看看json.load的返回类型。 print(type(data)) will show, it is actually a list . print(type(data))会显示,它实际上是一个list For your specific example对于您的具体示例

for entry in data:
    print(entry['show']['genres'])

will do the trick.会做的伎俩。

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

相关问题 'TypeError:列表索引必须是整数,而不是str'Python 3 - 'TypeError: list indices must be integers, not str' Python 3 TypeError:list indices必须是整数,而不是str Python - TypeError: list indices must be integers, not str Python TypeError:列表索引必须是整数,而不是Python中的str - TypeError: list indices must be integers, not str in 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-> 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