Python Spotipy:打印轨迹信息
我无法使用spotipy在Spotify上打印曲目中的信息 我目前拥有以下代码:Python Spotipy:打印轨迹信息,python,list,python-3.x,dictionary,Python,List,Python 3.x,Dictionary,我无法使用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 它输出: [{'external_
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
它输出:
[{'external_url':{'spotify':'''},
“href”:”,
“id”:“0YWxKQj2Go9CGHCp77UOyy”,
“name”:“Fabolous”,
“类型”:“艺术家”,
'uri':'spotify:artist:0YWxKQj2Go9CGHCp77UOyy'}]
当我尝试使用Artister_name=track_info['album']['artists']并在末尾添加['name]时,如下所示:
artist_name = track_info['album']['artists']['name']
我得到这个错误:
TypeError:列表索引必须是整数或片,而不是str
我真的不知道为什么当它是一个字符串时会这样说。
曲目信息['album']['artists']
是一个列表,您需要使用索引获取项目(列表[0]
):
它可以是多个艺术家。在这种情况下,请使用:
artist_name = track_info['album']['artists'][0]['name']
artist_names = [artist['name'] for artist in track_info['album']['artists']]