Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to access the Atlas Observatory of Economic Complexity API at this location: http://atlas.media.mit.edu/about/api/data/

Using the following code

import pandas as pd
import numpy as np
import matplotlib as mpl
from urllib2 import urlopen
import csv as csv
import json


url = "http://atlas.media.mit.edu/hs/export/2010/show/all/all/"
mydata=open(urllib2.urlopen(url))
response = json.loads(mydata)

To which I get the following error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-38-fbe696d9098d> in <module>()
      8 
      9 url = "http://atlas.media.mit.edu/hs/export/2010/show/all/all/"
---> 10 mydata=open(urlopen(url))
     11 response = json.loads(mydata)
     12 

TypeError: coercing to Unicode: need string or buffer, instance found

I'm new to accessing data from API's via python so there may be something simple I am missing. The goal is to get trade data and put it into a pandas data frame. As a bonus if anyone has any good sources for working with APIs in python please let me know.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

I recommend using requests, http://docs.python-requests.org/en/latest/. It can be installed via pip and is a very clean interface built on top of urllib2.

import requests
url = "http://atlas.media.mit.edu/hs/export/2010/show/all/all/"
response = requests.get(url)
mydata = response.text
share|improve this answer
    
Thanks, that answers my question. However, what do I do with the data in mydata at the end...I just get "u'{\n "data": [\n {\n "export_val": 44456016199.05,\n" and so on. How do I get that into a tabular format such as a data frame? –  tomc4yt Sep 30 '14 at 17:07
1  
can import json and call json.loads() on the json string to yield a dictionary, then use pandas.DataFrame.from_dict() to put it into pandas. –  ragingSloth Sep 30 '14 at 19:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.