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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

nicky sAuthor Commented:
Can you give it for python 3
0
NorieAnalyst Assistant Commented:
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
NorieAnalyst Assistant Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.