python - Best way to get info out of dictionary -
i struggling wrap head around best way title, artist name out following list.
{'status': 'ok', 'results': [{'score': 0.94222, 'id': 'ca222fc1-d1ed-4c30-b21f-eb533cc909aa', 'recordings': [{'artists': [{'id': '84dc4f23-c0b8-4fe1-bbca-a3993ddc8fc2', 'name': 'primus'}], 'id': '2f6227ba-e061-48b2-a700-15e209ed3650', 'title': 'wynona’s big brown beaver', 'duration': 264}, {'artists': [{'id': '84dc4f23-c0b8-4fe1-bbca-a3993ddc8fc2', 'name': 'primus'}], 'id': '520ebd07-d12c-429c-a137-19d13303a706', 'title': 'wynona’s big brown beaver', 'duration': 261}, {'artists': [{'id': '84dc4f23-c0b8-4fe1-bbca-a3993ddc8fc2', 'name': 'primus'}], 'id': '757439ce-58b1-4808-af3b-3b90092890c1', 'title': 'wynona’s big brown beaver', 'duration': 262}, {'artists': [{'id': '84dc4f23-c0b8-4fe1-bbca-a3993ddc8fc2', 'name': 'primus'}], 'id': 'a432eaa4-e5bb-414a-9595-cdaa46d48d6e', 'title': 'wynona’s big brown beaver', 'duration': 264}, {'artists': [{'id': '84dc4f23-c0b8-4fe1-bbca-a3993ddc8fc2', 'name': 'primus'}], 'id': 'c344a3f9-1755-4f1b-b227-8a5918713466', 'title': 'wynona’s big brown beaver', 'duration': 261}, {'artists': [{'id': '84dc4f23-c0b8-4fe1-bbca-a3993ddc8fc2', 'name': 'primus'}], 'id': 'e658eb35-9d15-45d8-965c-1abcb19c8bc9', 'title': 'wynona’s big brown beaver', 'duration': 264}]}]}
i using acoustic.match match songs , above list returned.
there has more elegant how doing is:
print(song['results'][0]['recordings'][0]['title'])
any pointers?
*****edit***** - in further review - there example code trying follow - can't seem work.
for score, recording_id, title, artist in acoustid.match(apikey, path):
acoustid.match returns dictionary/list above.
if dict bothering you, may want convert class.
class music(): def __init__(self, song): self.title = song['results'][0]['recordings'][0]['title'] self.artist = song['results'][0]['recordings'][0]['artist'][0]['name'] # , on , forth new_song = music(song) >>> print new_song.artist primus >>> print new_song.title wynona’s big brown beaver
Comments
Post a Comment