Python 将所有标签存储在艺术家[i][';标签';]
我有下面的代码从上一个fm api获取艺术家标签(流派)。每个艺术家可以有许多标签,如:Python 将所有标签存储在艺术家[i][';标签';],python,json,Python,Json,我有下面的代码从上一个fm api获取艺术家标签(流派)。每个艺术家可以有许多标签,如: {'artist': {'tags': {'tag': [{'name': 'alternative', 'url': 'https://www.last.fm/tag/alternative'}, {'name': 'indie', 'url': 'https://www.last.fm/tag/indie'}, {'name': 'electronic', 'url': 'https:
{'artist': {'tags': {'tag': [{'name': 'alternative',
'url': 'https://www.last.fm/tag/alternative'}, {'name': 'indie',
'url': 'https://www.last.fm/tag/indie'}, {'name': 'electronic',
'url': 'https://www.last.fm/tag/electronic'}]},
我想将艺术家的所有标签存储在艺术家[I]['tags']
中。问题是,我刚刚得到了最后一个带有此代码的标记:
for artist in artist_data['artist']['tags']["tag"]:
tags = artist["name"]
artists[i]['tags'] = tags
print(artists[i])
更多相关代码:
artists = {}
for i,v in artists.items():
chosen = artists[i]['name'].replace(" ", "+")
artist_response = requests.get('http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&format=json&artist='+chosen+'&api_key='+api_key)
artist_data = artist_response.json()
for artist in artist_data['artist']['tags']["tag"]:
tags = artist["name"]
artists[i]['tags'] = tags
print(artists[i])
您知道如何在
artists[i]['tags']
中存储所有标记吗?您正在用新的标记替换每个循环中的artists[i]['tags']
。
您可能希望像这样附加到它:
artists[i]['tags'].附加(标签)
您必须在循环之前创建artists[i]['tags']=[]