我是python3的新手,我在尝试阅读上一天的地震数据时遇到以下错误!
Traceback (most recent call last):
File "Z:\Python learning\Up and run\Exercise Files\Ch5\jsondata_finished.py", line 54, in <module>
main()
File "Z:\Python learning\Up and run\Exercise Files\Ch5\jsondata_finished.py", line 49, in main
printResults(data)
File "Z:\Python learning\Up and run\Exercise Files\Ch5\jsondata_finished.py", line 33, in printResults
if (feltReports != None) & (feltReports > 0):
TypeError: unorderable types: NoneType() > int()
我无法识别错误。这是我的代码:
import urllib.request
import json
def printResults(data):
# Use the json module to load the string data into a dictionary
theJSON = json.loads(data.decode())
# 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"])
# 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 only the events where at least 1 person reported feeling something
print ("Events that were felt:")
for i in theJSON["features"]:
feltReports = i["properties"]["felt"]
if (feltReports != None) & (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 = "http://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 (webUrl.getcode())
if (webUrl.getcode() == 200):
data = webUrl.read()
# print out our customized results
printResults(data)
else:
print ("Received an error from server, cannot retrieve results " + str(webUrl.getcode()))
if __name__ == "__main__":
main()
请帮忙!我试着通过查看其他用户的解决方案做了一些事情,但我仍然一遍又一遍地得到同样的错误。
答案 0 :(得分:1)
使用and
代替&
。另外,使用is not None
检查对象是否不是None
而不是!= None