猿问

Python 在使用 JSON 进行 for 循环后停止运行

我正在用 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 循环。


我在这里缺少什么吗?谢谢!


慕森卡
浏览 135回答 1
1回答

宝慕林4294392

你的代码运行良好。尝试检查代码的缩进。也许你需要它。这是你的结果:result code: 200USGS Magnitude 2.5+ Earthquakes, Past Day48 events recorded112 km E of Chignik, Alaska3 km SW of Indios, Puerto Ricosouth of the Fiji Islands10 km SSW of Port-Olry, Vanuatu11 km E of Nueva Concepción, Guatemala8 km ESE of La Parguera, Puerto Rico27 km NW of Londres, ArgentinaSouth Shetland Islands23km NW of Ludlow, CA56 km ENE of Pedro Bay, Alaska4 km SE of Maria Antonia, Puerto Rico2 km E of Magas Arriba, Puerto Rico133 km S of Isangel, Vanuatu155 km SSE of Sand Point, Alaska8 km WSW of Volcano, Hawaii9 km WSW of Volcano, Hawaii5 km SSW of Pāhala, Hawaii107 km SSE of Sand Point, Alaska8 km WSW of Volcano, Hawaii8 km WSW of Volcano, Hawaii5 km E of La Parguera, Puerto Rico4 km ESE of Maria Antonia, Puerto Rico82 km SW of Kaktovik, Alaska136 km SSE of Sand Point, Alaska32 km E of Balmorhea, Texassouth of the Fiji Islands16 km WNW of Clayton, Idaho16 km WNW of Clayton, Idaho16 km WNW of Clayton, Idaho8km SW of Niland, CAWest Chile Rise33 km SW of Ashkāsham, Afghanistan23 km NNE of Golconda, NevadaSouthern Alaska2 km S of Magas Arriba, Puerto Rico123 km SE of Sand Point, Alaska43 km SSW of Beaver, Alaska66 km SE of Akutan, Alaska7 km N of Nardin, Oklahoma2 km SSE of Magas Arriba, Puerto Rico38 km ESE of Nikolski, Alaska82 km WNW of El Alto, Perunorthern Mid-Atlantic RidgeWest Chile Rise7 km N of Nardin, Oklahomasouth of the Fiji Islands113 km SSE of Sand Point, Alaskanorthern Mid-Atlantic Ridge--------------4.6 south of the Fiji Islands5.2 10 km SSW of Port-Olry, Vanuatu4.2 11 km E of Nueva Concepción, Guatemala5.0 27 km NW of Londres, Argentina5.0 South Shetland Islands4.3 133 km S of Isangel, Vanuatu4.3 155 km SSE of Sand Point, Alaska4.6 107 km SSE of Sand Point, Alaska4.3 136 km SSE of Sand Point, Alaska6.1 south of the Fiji Islands4.7 West Chile Rise4.4 33 km SW of Ashkāsham, Afghanistan4.4 66 km SE of Akutan, Alaska4.0 38 km ESE of Nikolski, Alaska4.6 82 km WNW of El Alto, Peru5.0 northern Mid-Atlantic Ridge6.0 West Chile Rise4.7 south of the Fiji Islands4.0 113 km SSE of Sand Point, Alaska5.3 northern Mid-Atlantic Ridge--------------Events that were felt:5.2 10 km SSW of Port-Olry, Vanuatu  reported 3 times4.2 11 km E of Nueva Concepción, Guatemala  reported 2 times3.0 9 km WSW of Volcano, Hawaii  reported 9 times2.8 5 km SSW of Pāhala, Hawaii  reported 1 times2.7 8 km WSW of Volcano, Hawaii  reported 6 times3.2 4 km ESE of Maria Antonia, Puerto Rico  reported 6 times2.8 16 km WNW of Clayton, Idaho  reported 2 times2.9 8km SW of Niland, CA  reported 1 times3.3 7 km N of Nardin, Oklahoma  reported 6 times3.0 2 km SSE of Magas Arriba, Puerto Rico  reported 1 times3.2 7 km N of Nardin, Oklahoma  reported 2 times
随时随地看视频慕课网APP

相关分类

Python
我要回答