0

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!

1
  • Bump. If you have found the answer yourself, you can still answer your own question. Commented Apr 22, 2014 at 9:36

2 Answers 2

1

For this kind of task you should use the Requests package. That way you can properly set your header:

headers = {'X-RJM-API-Key': 'myapikey', 'content-type: text/csv'}
r = requests.get('https://api.rjmetrics.com/0.1/chart/chartid/export', headers=headers)

and then use the default csv reader.

Sign up to request clarification or add additional context in comments.

Comments

0

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'))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.