Load json data in Python 3.3 a little from Python 2.7
Here I gave two examples using Json data from an array and from url.
Example1.py: load json data from an array in Python3.3
import json
data = [ { 'a':'A', 'b':(2, 4), 'c':3.0 } ]
data_string = json.dumps(data)
print(data_string)
data_load = json.loads(data_string)
print(data_load)
print(data_load[0]['a'])
return:
[{"a": "A", "b": [2, 4], "c": 3.0}]
[{'a': 'A', 'c': 3.0, 'b': [2, 4]}]
A
Reference:
http://pymotw.com/2/json/
Example2.py, load json data from url (earthquake data)
#http://earthquake.usgs.gov/earthquakes/feed/v1.0/geojson.php
#past 30days M4.5 earthquake
#http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_month.geojson
import urllib.request
import json
geourl = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_month.geojson"
response = urllib.request.urlopen(geourl)
content = response.read()
data = json.loads(content.decode("utf8"))
print(data)
print(data['type'])
Video:Load json data in Python 3.3
No comments:
Post a Comment