Need to extract json data from a site in ascending order

How to do following scripting in python ?

USGS (US Geological Survey) publishes various earthquake data as JSON feed. Here’s a feed spanning all domestic earthquakes from the past week:
https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson

Using this JSON feed:
1) identify every earthquake in California from past week,
2) list them chronologically (ascending),
3. and finally output in a format resembling the following e.g.:

2017-07-13T20:43:37+00:00 | 3km NW of Greenville, California | Magnitude: 1
2017-07-13T22:09:53+00:00 | 41km SW of Ferndale, California | Magnitude: 2.76
2017-07-13T22:31:04+00:00 | 11km E of Mammoth Lakes, California | Magnitude: 1.31
2017-07-13T22:32:48+00:00 | 15km SE of Mammoth Lakes, California | Magnitude: 0.92
2017-07-13T22:37:52+00:00 | 12km E of Mammoth Lakes, California | Magnitude: 0.95
2017-07-13T22:45:28+00:00 | 37km SE of Bridgeport, California | Magnitude: 1.7
2017-07-13T22:49:58+00:00 | 8km ENE of Mammoth Lakes, California | Magnitude: 0.92
2017-07-13T22:54:30+00:00 | 3km SE of Atascadero, California | Magnitude: 2.04
nicky sAsked:
Who is Participating?
 
NorieVBA ExpertCommented:
Give this a try, which is for Python 2.7.
import urllib, json, datetime

url = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson"

response = urllib.urlopen(url)

data = json.loads(response.read())

for feature in data["features"]:
	location = feature["properties"]["place"]
	time = datetime.datetime.fromtimestamp(feature["properties"]["time"]/1000).strftime('%Y-%m-%d %H:%M:%S')
	mag = feature["properties"]["mag"]
	
	if "CA" in location:
		print "{0}|{1}|Magnitude {2}".format(time, location, mag)

Open in new window

0
 
nicky sAuthor Commented:
Getting error

runfile('C:/Users/Admin/Documents/Python Scripts/json_ascending.py', wdir='C:/Users/Admin/Documents/Python Scripts')
Traceback (most recent call last):

  File "<ipython-input-11-8cb9377f4fbe>", line 1, in <module>
    runfile('C:/Users/Admin/Documents/Python Scripts/json_ascending.py', wdir='C:/Users/Admin/Documents/Python Scripts')

  File "C:\Users\Admin\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 714, in runfile
    execfile(filename, namespace)

  File "C:\Users\Admin\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 89, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/Admin/Documents/Python Scripts/json_ascending.py", line 5, in <module>
    response = urllib.urlopen(url)

AttributeError: module 'urllib' has no attribute 'urlopen'
0
 
nicky sAuthor Commented:
After making this chnage

import urllib.request, json, datetime

url = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson"

response = urllib.request.urlopen(url)


I got new error


runfile('C:/Users/Admin/Documents/Python Scripts/json_ascending.py', wdir='C:/Users/Admin/Documents/Python Scripts')
Traceback (most recent call last):

  File "<ipython-input-13-8cb9377f4fbe>", line 1, in <module>
    runfile('C:/Users/Admin/Documents/Python Scripts/json_ascending.py', wdir='C:/Users/Admin/Documents/Python Scripts')

  File "C:\Users\Admin\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 714, in runfile
    execfile(filename, namespace)

  File "C:\Users\Admin\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 89, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/Admin/Documents/Python Scripts/json_ascending.py", line 7, in <module>
    data = json.loads(response.read())

  File "C:\Users\Admin\Anaconda3\lib\json\__init__.py", line 312, in loads
    s.__class__.__name__))

TypeError: the JSON object must be str, not 'bytes'
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
nicky sAuthor Commented:
Can you give it for python 3
0
 
NorieVBA ExpertCommented:
This works for Python 3.6.1.
import urllib.request, json, datetime

url = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson"

response = urllib.request.urlopen(url)

data = json.loads(response.read())

for feature in data["features"]:
	location = feature["properties"]["place"]
	time = datetime.datetime.fromtimestamp(feature["properties"]["time"]/1000).strftime('%Y-%m-%d %H:%M:%S')
	mag = feature["properties"]["mag"]
	
	if "CA" in location:
		print ("{0}|{1}|Magnitude {2}".format(time, location, mag))

Open in new window

0
 
nicky sAuthor Commented:
Still getting error

runfile('C:/Users/Admin/Documents/Python Scripts/json_ascending.py', wdir='C:/Users/Admin/Documents/Python Scripts')
Traceback (most recent call last):

  File "<ipython-input-18-8cb9377f4fbe>", line 1, in <module>
    runfile('C:/Users/Admin/Documents/Python Scripts/json_ascending.py', wdir='C:/Users/Admin/Documents/Python Scripts')

  File "C:\Users\Admin\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 714, in runfile
    execfile(filename, namespace)

  File "C:\Users\Admin\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 89, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/Admin/Documents/Python Scripts/json_ascending.py", line 7, in <module>
    data = json.loads(response.read())

  File "C:\Users\Admin\Anaconda3\lib\json\__init__.py", line 312, in loads
    s.__class__.__name__))

TypeError: the JSON object must be str, not 'bytes'
0
 
NorieVBA ExpertCommented:
How exactly are you using what I posted?
0
 
nicky sAuthor Commented:
Copying your code in anaconda spyder and executing

attaching image

import urllib.request, json, datetime

url = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson"


response = urllib.request.urlopen(url)

data = json.loads(response.read())

for feature in data["features"]:
	location = feature["properties"]["place"]
	time = datetime.datetime.fromtimestamp(feature["properties"]["time"]/1000).strftime('%Y-%m-%d %H:%M:%S')
	mag = feature["properties"]["mag"]
	
	if "CA" in location:
		print ("{0}|{1}|Magnitude {2}".format(time, location, mag))

Open in new window



Capture1.JPG
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.