Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Spotipy:打印轨迹信息_Python_List_Python 3.x_Dictionary - Fatal编程技术网

Python Spotipy:打印轨迹信息

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_

我无法使用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_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']]