为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕森卡 2023-12-29 15:41:37
我正在用 Python 做一个 JSON 教程,我意识到代码直到最后才运行。这是代码:## Example file for parsing and processing JSON#import urllib.request  # instead of urllib2 like in Python 2.7import jsondef 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 循环。我在这里缺少什么吗?谢谢!
查看完整描述

1 回答

?
宝慕林4294392

TA贡献2021条经验 获得超8个赞

你的代码运行良好。尝试检查代码的缩进。也许你需要它。


这是你的结果:


result code: 200

USGS Magnitude 2.5+ Earthquakes, Past Day

48 events recorded

112 km E of Chignik, Alaska

3 km SW of Indios, Puerto Rico

south of the Fiji Islands

10 km SSW of Port-Olry, Vanuatu

11 km E of Nueva Concepción, Guatemala

8 km ESE of La Parguera, Puerto Rico

27 km NW of Londres, Argentina

South Shetland Islands

23km NW of Ludlow, CA

56 km ENE of Pedro Bay, Alaska

4 km SE of Maria Antonia, Puerto Rico

2 km E of Magas Arriba, Puerto Rico

133 km S of Isangel, Vanuatu

155 km SSE of Sand Point, Alaska

8 km WSW of Volcano, Hawaii

9 km WSW of Volcano, Hawaii

5 km SSW of Pāhala, Hawaii

107 km SSE of Sand Point, Alaska

8 km WSW of Volcano, Hawaii

8 km WSW of Volcano, Hawaii

5 km E of La Parguera, Puerto Rico

4 km ESE of Maria Antonia, Puerto Rico

82 km SW of Kaktovik, Alaska

136 km SSE of Sand Point, Alaska

32 km E of Balmorhea, Texas

south of the Fiji Islands

16 km WNW of Clayton, Idaho

16 km WNW of Clayton, Idaho

16 km WNW of Clayton, Idaho

8km SW of Niland, CA

West Chile Rise

33 km SW of Ashkāsham, Afghanistan

23 km NNE of Golconda, Nevada

Southern Alaska

2 km S of Magas Arriba, Puerto Rico

123 km SE of Sand Point, Alaska

43 km SSW of Beaver, Alaska

66 km SE of Akutan, Alaska

7 km N of Nardin, Oklahoma

2 km SSE of Magas Arriba, Puerto Rico

38 km ESE of Nikolski, Alaska

82 km WNW of El Alto, Peru

northern Mid-Atlantic Ridge

West Chile Rise

7 km N of Nardin, Oklahoma

south of the Fiji Islands

113 km SSE of Sand Point, Alaska

northern Mid-Atlantic Ridge

--------------


4.6 south of the Fiji Islands

5.2 10 km SSW of Port-Olry, Vanuatu

4.2 11 km E of Nueva Concepción, Guatemala

5.0 27 km NW of Londres, Argentina

5.0 South Shetland Islands

4.3 133 km S of Isangel, Vanuatu

4.3 155 km SSE of Sand Point, Alaska

4.6 107 km SSE of Sand Point, Alaska

4.3 136 km SSE of Sand Point, Alaska

6.1 south of the Fiji Islands

4.7 West Chile Rise

4.4 33 km SW of Ashkāsham, Afghanistan

4.4 66 km SE of Akutan, Alaska

4.0 38 km ESE of Nikolski, Alaska

4.6 82 km WNW of El Alto, Peru

5.0 northern Mid-Atlantic Ridge

6.0 West Chile Rise

4.7 south of the Fiji Islands

4.0 113 km SSE of Sand Point, Alaska

5.3 northern Mid-Atlantic Ridge

--------------




Events that were felt:

5.2 10 km SSW of Port-Olry, Vanuatu  reported 3 times

4.2 11 km E of Nueva Concepción, Guatemala  reported 2 times

3.0 9 km WSW of Volcano, Hawaii  reported 9 times

2.8 5 km SSW of Pāhala, Hawaii  reported 1 times

2.7 8 km WSW of Volcano, Hawaii  reported 6 times

3.2 4 km ESE of Maria Antonia, Puerto Rico  reported 6 times

2.8 16 km WNW of Clayton, Idaho  reported 2 times

2.9 8km SW of Niland, CA  reported 1 times

3.3 7 km N of Nardin, Oklahoma  reported 6 times

3.0 2 km SSE of Magas Arriba, Puerto Rico  reported 1 times

3.2 7 km N of Nardin, Oklahoma  reported 2 times



查看完整回答
反对 回复 2023-12-29
  • 1 回答
  • 0 关注
  • 134 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信