简体   繁体   中英

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:

TypeError: list indices must be integers, not 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 . print(type(data)) will show, it is actually a list . For your specific example

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

will do the trick.

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