我正在用 Python 做一个 JSON 教程,我意识到代码直到最后才运行。
这是代码:
#
# Example file for parsing and processing JSON
#
import urllib.request # instead of urllib2 like in Python 2.7
import json
def printResults(data):
# Use the json module to load the string data into a dictionary
theJSON = json.loads(data)
# now we can access the contents of the JSON like any other Python object
if "title" in theJSON["metadata"]:
print(theJSON["metadata"]["title"])
# output the number of events, plus the magnitude and each event name
count = theJSON["metadata"]["count"]
print(str(count) + " events recorded")
# for each event, print the place where it occurred
for i in theJSON["features"]:
print(i["properties"]["place"])
# # # code doesn't work from here # # #
print("--------------\n")
# # print the events that only have a magnitude greater than 4
for i in theJSON["features"]:
if i["properties"]["mag"] >= 4.0:
print("%2.1f" % i["properties"]["mag"], i["properties"]["place"])
print("--------------\n")
# # print only the events where at least 1 person reported feeling something
print("\n\nEvents that were felt:")
for i in theJSON["features"]:
feltReports = i["properties"]["felt"]
if (feltReports != None):
if (feltReports > 0):
print("%2.1f" % i["properties"]["mag"], i["properties"]["place"], " reported " + str(feltReports) + " times")
def main():
# define a variable to hold the source URL
# In this case we'll use the free data feed from the USGS
# This feed lists all earthquakes for the last day larger than Mag 2.5
urlData = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson"
我知道这不是缩进,因为我可以单独运行每个 for 循环,即如果我注释第一个 for 循环,则运行第二个 for 循环。
我在这里缺少什么吗?谢谢!
宝慕林4294392
相关分类