gpt4 book ai didi

python - 从 lastfm : TypeError: string indices must be integers 获取轨道的 mbid

转载 作者:太空宇宙 更新时间:2023-11-03 14:28:07 26 4
gpt4 key购买 nike

我有轨道{},它使用最新的 fm API 存储有关轨道的一些信息。现在我想将每个轨道的 mbid 存储在 Tracks{} 中,但出现错误:

  mbid = track['mbid']
TypeError: string indices must be integers

print(track) 返回:

name
mbid
url
duration
....

print(track_mbid_response) 具有以下格式:

{'track': {'name': 'Achtung Baby', 'mbid'.....: 

你知道问题出在哪里吗?

for i, v in tracks.items():
artist = tracks[i]['artist'].replace(" ", "+")
title = tracks[i]['title'].replace(" ", "+")


track_mbid = requests.get('http://ws.audioscrobbler.com/2.0/?method=track.getInfo&format=json&api_key=' + api_key + '&artist=' + artist + '&track=' + title)
track_mbid_response = track_mbid.json()

#print(track_mbid_response)

for track in track_mbid_response['track']:
#print(track)

mbid = track['mbid']
print(mbid)

tracks[ID]['mbid'] = tracks[i]['mbid']

最佳答案

您正在迭代轨道中的。只需执行 track = track_mbid_response["track"] 然后您就可以添加到该字典中。

关于python - 从 lastfm : TypeError: string indices must be integers 获取轨道的 mbid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47481659/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com