10000 Working with JSON data · neryuuk/learning-python@4370ae0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4370ae0

Browse files
committed
Working with JSON data
1 parent fbf19fe commit 4370ae0

File tree

2 files changed

+51
-81
lines changed

2 files changed

+51
-81
lines changed

Ch5 - Internet Data/jsondata_finished.py

Lines changed: 0 additions & 63 deletions
This file was deleted.

Ch5 - Internet Data/jsondata_start.py

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,72 @@
1-
#
2-
# Example file for parsing and processing JSON
3-
# LinkedIn Learning Python course by Joe Marini
4-
#
1+
import urllib.request
2+
import json
53

6-
import urllib.request
74

85
def printResults(data):
96
# Use the json module to load the string data into a dictionary
10-
theJSON = json.loads(data)
11-
7+
content = json.loads(data)
8+
129
# now we can access the contents of the JSON like any other Python object
10+
if "metadata" in content and "title" in content["metadata"]:
11+
print(content["metadata"]["title"])
12+
print("---------------------------------------------\n")
1313

14-
15-
# output the number of events, plus the magnitude and each event name
14+
# output the number of events, plus the magnitude and each event name
15+
if "metadata" in content and "count" in content["metadata"]:
16+
print("{} event(s) recorded".format(content["metadata"]["count"]))
17+
print("---------------------------------------------\n")
1618

17-
1819
# for each event, print the place where it occurred
19-
20+
if "features" in content:
21+
for event in content["features"]:
22+
if "properties" in event and "place" in event["properties"]:
23+
print(event["properties"]["place"])
24+
print("---------------------------------------------\n")
2025

2126
# print the events that only have a magnitude greater than 4
22-
27+
print("Events with magnitude greater than 4:\n")
28+
if "features" in content:
29+
for event in content["features"]:
30+
if "properties" in event and "mag" in event["properties"] and event["properties"]["mag"] > 4:
31+
print("{}, Magnitude {}".format(
32+
event["properties"]["place"],
33+
event["properties"]["mag"]
34+
))
35+
print("---------------------------------------------\n")
2336

2437
# print only the events where at least 1 person reported feeling something
38+
print("Events that were felt by someone:\n")
39+
if "features" in content:
40+
for event in content["features"]:
41+
if "properties" in event and "felt" in event["properties"]:
42+
felt = event["properties"]["felt"]
43+
if felt and int(felt) > 0:
44+
print("{}, felt {} time{}".format(
45+
event["properties"]["place"],
46+
felt,
47+
"s" if felt > 1 else ""
48+
))
49+
print("---------------------------------------------\n")
50+
2551

26-
2752
def main():
2853
# define a variable to hold the source URL
2954
# In this case we'll use the free data feed from the USGS
3055
# This feed lists all earthquakes for the last day larger than Mag 2.5
31-
urlData = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson"
56+
url = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson"
57+
58+
try:
59+
# Open the URL and read the data
60+
website = urllib.request.urlopen(url)
61+
httpStatus = website.getcode()
62+
print("GET {} {}".format(url, httpStatus))
63+
if httpStatus == 200:
64+
printResults(website.read())
65+
else:
66+
print("Received HTTP status {} from the server.".format(httpStatus))
67+
except Exception as e:
68+
print("Error opening website {}:\n{}".format(url, e))
3269

33-
# Open the URL and read the data
34-
webUrl = urllib.request.urlopen(urlData)
35-
print ("result code: " + str(webUrl.getcode()))
36-
3770

3871
if __name__ == "__main__":
3972
main()

0 commit comments

Comments
 (0)
0