我在使用spotipy从Spotify上打印信息时遇到了困难。
我目前有以下代码:
import spotipy
import sys
import json
urn = 'spotify:track:450vazRH94IB21mom5FkN9'
sp = spotipy.Spotify()
track_info = sp.track(urn)
artist_name = track_info['album']['artists']
artist_name它的产出如下:
[{‘外部_urls’:{'spotify':'https://open.spotify.com/artist/0YWxKQj2Go9CGHCp77UOyy'},'href':'https://api.spotify.com/v1/artists/0YWxKQj2Go9CGHCp77UOyy','id':'0YWxKQj2Go9CGHCp77UOyy','name':'Fabolous','type':‘艺人’,'uri':‘spotify:艺人:0YxKQj2Go9CGHCp77UOyy’}]
当我尝试使用artist_name =track_info‘相册’并在末尾添加'name‘时,如下所示:
artist_name = track_info['album']['artists']['name']我知道这个错误:
TypeError:列表索引必须是整数或切片,而不是str
我不太清楚为什么当它是一根线的时候会这么说。
发布于 2017-05-04 03:10:13
track_info['album']['artists']是一个列表,需要使用索引(list[0])获取项目:
artist_name = track_info['album']['artists'][0]['name']可以是多位艺术家。在这种情况下使用列表理解
artist_names = [artist['name'] for artist in track_info['album']['artists']]https://stackoverflow.com/questions/43773138
复制相似问题