How to Convert Soundcloud API Responses into CSV in Python?
Question: I, a newbie, am playing around with the Soundcloud API in Python, and when I
get a chunk of data in return (as an object, I'm guessing) I am not really
sure how to manipulate it:

    
    
    favorites = client.get('/me/favorites')
    <class 'soundcloud.resource.ResourceList'>
    

I'm guessing this is sort of of a package of data, which has its own
attributes. I can do:

    
    
    for favorite in favorites:
        print favorite.id
    

And this will give me a list of the IDs of the songs I have favorited.

    
    
    146967741
    136766472
    

But, how can I see what attributes this object has for me to play with, and
more importantly, how can I convert this into a CSV that I can look into?

Thanks!


Answer: Assuming that you're using soundcloud-python you can use the `fields` method
to get the internal dict or `keys` to just look at the available names.
Source: [resource.py](https://github.com/soundcloud/soundcloud-
python/blob/master/soundcloud/resource.py)

As for the csv question, it would be helpful to have more information about
what you've already tried, but you could use the csv module's `DictWriter`
class, and pass it the dictionaries from `fields()`. More details are
available in the [python
documentation](https://docs.python.org/2/library/csv.html#csv.DictWriter).

Putting the two together:

    
    
    import csv
    with open('names.csv', 'w') as csvfile:
        fieldnames = favorites[0].keys()
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    
        writer.writeheader()
    
        for favorite in favorites:
            writer.writerow(favorite.fields())
    

Keep in mind I haven't tested this and I don't have an API key. The
[SoundCloud API Guide](https://developers.soundcloud.com/docs/api/guide) also
has a wealth of valuable information.

