pythonlistapiindexingdiscogs-api

Parsing from a list in python


I just finished some online tutorial and i was experimenting with Discogs Api

My code is as follows :

import discogs_client as discogs

discogs.user_agent = '--' #i removed it 

artist_input = raw_input("Enter artist\'s name : ")
artist = discogs.Artist(artist_input)

if artist._response.status_code in (400, 401, 402, 403, 404):
    print "Sorry, this artist %s does not exist in Discogs database" % (artist_input)
elif artist._response.status_code in (500, 501, 502, 503) :  
    print "Server is a bit sleepy, give it some time, won\'t you ._."
else:
        print "Artist : " + artist.name
        print "Description : " + str(artist.data["profile"])
        print "Members :",
        members = artist.data["members"]
        for index, name in enumerate(members):
            if index == (len(members) - 1):
                print name + "."
            else:    
                print name + ",",

The list's format i want to work with, is like this:

[<MasterRelease "264288">, <MasterRelease "10978">,  <Release "4665127">...

I want to isolate those with MasterRelease, so i can get their ids

I tried something like

for i in artist.releases:
        if i[1:7] in "Master":
            print i

or

for i in thereleases:
        if i[1:7] == "Master":
       print i

I;m certainly missing something, but it buffles me since i can do this

newlist = ["<abc> 43242"]
print newlist[0][1]  

and in this scenario

thereleases = artist.releases    
print thereleases[0][1]

i get

TypeError: 'MasterRelease' object does not support indexing

Feel free to point anything about the code, since i have limited python knowledge yet.


Solution

  • You are looking at the Python representation of objects, not strings. Each element in the list is an instance of the MasterRelease class.

    These objects have an id attribute, simply refer to the attribute directly:

    for masterrelease in thereleases:
        print masterrelease.id
    

    Other attributes are .versions for other releases, .title for the release title, etc.