Python Forum

Full Version: can´t store the mbid - `KeyError: 'mbid'`.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm getting the tracks of some albums from lastfm. Then for each track I'm getting some information (title, number, artist, etc). Then, I want also to get the mbid of each track, but here I have an error:

KeyError: 'mbid'.

Get and store tracks information from albums is working fine:

 tracks = {}
    for i, v in albums.items():
        artist = albums[i]['artist'].replace(" ", "+")
        name = albums[i]['name'].replace(" ", "+")
        album_response_data = requests.get(
            'http://ws.audioscrobbler.com/2.0/?method=album.getinfo&format=json&api_key=' + api_key + '&artist=' + artist + '&album=' + name)
        album_response = album_response_data.json()    
    
        if 'album' in album_response.keys():
            for album in album_response['album']['tracks']['track']:
                title = album['name']
      
                tracks[ID] = {} 
                tracks[ID]['trackID'] = ID
                tracks[ID]['title'] = title
                tracks[ID]['number'] = number
                tracks[ID]['artist'] = albums[i]['artist']
                tracks[ID]['album'] = albums[i]['name']
                tracks[ID]['albumID'] = albums[i]['ID']
                   
                ID += 1
Then getting and store the mbid of each track is where the issue is:

   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.keys())
        #print(track_mbid_response)
    
        track_data = track_mbid_response["track"]
    
        #for track in track_data:
            #print(track)
        mbid = track_data['mbid']
    
    
        tracks[i]['mbid'] = mbid
    
    
    print(tracks)
print(track_mbid):

<Response [200]>

print(track_mbid_response) returns on this format

{"track":{"name":"Brown Sugar","mbid":"8d2338a2-4acc-4d99-b2a7-1151f428cab6","url":"https://www.last.fm/music/The+Rolling+Stones/_/B...
Please show the fill traceback, verbatim.
It contains extremely valuable information for debugging.
Including line number where error is suspected to occur.
Sorry, but I didnt understand what is the fill traceback?

But the error is here:

mbid = track_data['mbid']
sorry typo should have been 'full traceback', it's the entire error message.
Thanks, it appears:

Traceback (most recent call last):
File "C:/Users/Ozzy/PycharmProjects/getData/getData.py", line 110, in <module>
mbid = track_data['mbid']
KeyError: 'mbid'
I will not merge thread now,as you have gotten answer.
You should not have started a new thread,as the task is same as here which also has more info.
Thanks and sorry, I didnt notice that the cause for the error, although it appears the same error information, was the same.
The error means: There is no key "mbid" in track_data.
You can not trust data from the internet.
You have to check each key for existence.


Sample:
#!/usr/bin/python3

dd = { "a": "aa" }

print(dd["a"])
print(dd["x"])   # <==  KeyError 'x'