[英]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.