Python Forum
can´t store the mbid - `KeyError: 'mbid'`.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
can´t store the mbid - `KeyError: 'mbid'`.
#1
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...
Reply
#2
Please show the fill traceback, verbatim.
It contains extremely valuable information for debugging.
Including line number where error is suspected to occur.
Reply
#3
Sorry, but I didnt understand what is the fill traceback?

But the error is here:

mbid = track_data['mbid']
Reply
#4
sorry typo should have been 'full traceback', it's the entire error message.
Reply
#5
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'
Reply
#6
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.
Reply
#7
Thanks and sorry, I didnt notice that the cause for the error, although it appears the same error information, was the same.
Reply
#8
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'
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020