我有这个功能可以audio_features
从Spotipy
.
def show_audio_features(artist):
tids = []
results = sp.recommendations(seed_artists = [artist['id']])
for track in results['tracks']:
tids.append(track['uri'])
features = sp.audio_features(tids)
return json.dumps(features, indent=4)
它打印:
12:51 - The Strokes
There, There - Radiohead
DARE - Gorillaz
Stand by Me - Oasis
Do You Realize?? - The Flaming Lips
The Scientist - Coldplay
Exit Music (For a Film) - Radiohead
Supremacy - Muse
[
{
"track_href": "https://api.spotify.com/v1/tracks/2rA36OZNb3LkvqcNro1ugK",
"analysis_url": "https://api.spotify.com/v1/audio-analysis/2rA36OZNb3LkvqcNro1ugK",
"energy": 0.776,
"liveness": 0.0884,
"tempo": 113.808,
"speechiness": 0.056,
"uri": "spotify:track:2rA36OZNb3LkvqcNro1ugK",
"acousticness": 0.0395,
"instrumentalness": 0.737,
"time_signature": 4,
"danceability": 0.693,
"key": 3,
"duration_ms": 319249,
"loudness": -9.804,
"valence": 0.666,
"type": "audio_features",
"id": "2rA36OZNb3LkvqcNro1ugK",
"mode": 1
}, etc
但显然,这个输出是不可迭代的。如果我尝试
for f in show_audio_features(artist):
print (f[0])
我得到:
TypeError: 'NoneType' object is not iterable
有没有办法可以阻止该spotipy
方法输出歌曲和艺术家,以便我可以遍历JSON
数据?