【发布时间】:2021-02-07 00:13:45
【问题描述】:
我正在用 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"
# Open the URL and read the data
webUrl = urllib.request.urlopen(urlData)
print("result code: " + str(webUrl.getcode()))
if (webUrl.getcode() == 200):
data = webUrl.read().decode("utf-8")
# print out our customized results
printResults(data)
else:
print("Received an error from server, cannot retrieve results " + str(webUrl.getcode()))
if __name__ == "__main__":
main()
代码在第一个循环之后立即停止:
# for each event, print the place where it occurred
for i in theJSON["features"]:
print(i["properties"]["place"])
我知道这不是缩进,因为我可以单独运行每个 for 循环,即如果我评论第一个 for 循环,则第二个 for 循环运行。
这里有什么我遗漏的吗?谢谢!
【问题讨论】:
-
我复制了你的代码,它似乎运行得很好。你认为什么不起作用?
-
您的代码运行良好。尝试检查代码的缩进。
-
可能是VS Code的问题。在我的调试控制台上,我只得到第一个 for 循环。我会看看我能做什么!谢谢,伙计们!