Having trouble trying to pull data from a url into python using an API
Question: I'm trying to pull data from a chart on a site in csv format. I've tried
different combinations of code but can't seem to figure it out. I keep getting
the following errors depending on the code I write:

TypeError: 'set' object is not subscriptable TypeError: a float is required

My latest attempt looked like this:

    
    
    import urllib   
    import urllib2    
    import csv    
    import StringIO
    
    
    url = "https://api.rjmetrics.com/0.1/chart/chartid/export"    
    headers = {"X-RJM-API-Key": "myapikey"}
    data = {"format=csv"}
    response = urllib2.Request(url, data, headers)    
    re = urllib2.urlopen(response)    
    spamreader = csv.reader(re, delimiter=',', quotechar='|')    
    for row in spamreader:    
        print row
    

The working CURL version looks like this:

    
    
    curl -d "format=csv" -H "X-RJM-API-Key: myapikey" https://api.rjmetrics.com/0.1/chart/chartid/export
    

but I don't know how to work with curl.

Thank you!


Answer: `data` should be a url encoded string, you are passing it a set --
{"format=csv"} is a set literal. Try this:

    
    
    data = urllib.urlencode(dict(format='csv'))
    

