diff --git a/Start/Ch_1/challenge_start.py b/Start/Ch_1/challenge_start.py index 8901a7a..c02259a 100644 --- a/Start/Ch_1/challenge_start.py +++ b/Start/Ch_1/challenge_start.py @@ -2,15 +2,65 @@ # Programming challenge: summarize the earthquake data import json +import pprint # for this challenge, we're going to summarize the earthquake data as follows: -# 1: How many quakes are there in total? -# 2: How many quakes were felt by at least 100 people? -# 3: Print the name of the place whose quake was felt by the most people, with the # of reports -# 4: Print the top 10 most significant events, with the significance value of each # open the data file and load the JSON with open("../../30DayQuakes.json", "r") as datafile: data = json.load(datafile) - \ No newline at end of file + + +def quake(q): + if q["properties"]["type"] == "earthquake": + return True + return False + + +def get_felt(record): + felt = record["properties"]['felt'] + if (felt is None): + felt = 0 + return float(felt) + + +def felt_filter(q): + felt_quake = q['properties']['felt'] + if felt_quake is not None and felt_quake >= 100: + return True + return False + + +def get_sig(q): + sig = q['properties']['sig'] + if sig is not None: + return float(sig) + return 0.0 + + +def get_most_significant(events): + events["features"].sort(key=get_sig, reverse=True) + return events["features"][0] + + +# 1: How many quakes are there in total? +quakes = list(filter(quake, data['features'])) +print(f"Total number of quakes: {len(quakes)}") +print(f"Total number of events: {data['metadata']['count']}") + +# 2: How many quakes were felt by at least 100 people? +popular = list(filter(felt_filter, quakes)) +print(f"Number of quakes felt by at least 100 people: {len(popular)}") + +# 3: Print the name of the place whose quake was felt by the most people, with the # of reports +most_felt = max(data["features"], key=get_felt) +print( + f"Most felt quake at {most_felt['properties']['title']}, reports: {most_felt['properties']['felt']}") + + +# 4: Print the top 10 most significant events, with the significance value of each +sig_events = sorted(data["features"], key=get_sig, reverse=True) +for i in range(0, 10): + print( + f"Event: {sig_events[i]['properties']['title']}, Significance: {sig_events[i]['properties']['sig']}") diff --git a/Start/Ch_1/filtering.py b/Start/Ch_1/filtering.py index b3ac348..89e261d 100644 --- a/Start/Ch_1/filtering.py +++ b/Start/Ch_1/filtering.py @@ -23,10 +23,26 @@ def filterUppers(x): chars = "abcDeFGHiJklmnoP" # TODO: use filter to remove items from a list +odds = list(filter(filterEvens, nums)) +lowers = list(filter(filterUppers, chars)) +#print(odds) +#print(lowers) # TODO: use filter on non-numeric sequence # Use the filter on our data - let's filter out all seismic events that were *not* quakes # open the data file and load the JSON -# with open("../../30DayQuakes.json", "r") as datafile: -# data = json.load(datafile) +with open("../../30DayQuakes.json", "r") as datafile: + data = json.load(datafile) + + +def notAQuake(q): + if q["properties"]["type"] == "earthquake": + return False + return True + + +events = list(filter(notAQuake, data["features"])) +print(f"Total non-quake events: {len(events)}") +for i in range(0, 10): + print(events[i]["properties"]["type"]) diff --git a/Start/Ch_1/minmax.py b/Start/Ch_1/minmax.py index f07afcc..404054e 100644 --- a/Start/Ch_1/minmax.py +++ b/Start/Ch_1/minmax.py @@ -9,14 +9,30 @@ # TODO: The min() function finds the minimum value - +# print(f"The minimum value is: {min(values)}") +# print(f"The minimum value is: {min(strings)}") # TODO: The max() function finds the maximum value - +# print(f"The minimum value is: {max(values)}") +# print(f"The minimum value is: {max(strings)}") # TODO: define a custom "key" function to extract a data field - +# print(f"The minimum value is: {min(strings, key=len)}") +# print(f"The minimum value is: {max(strings, key=len)}") # TODO: open the data file and load the JSON -# with open("../../30DayQuakes.json", "r") as datafile: -# data = json.load(datafile) +with open("../../30DayQuakes.json", "r") as datafile: + data = json.load(datafile) + print(data["metadata"]["title"]) + print(len(data["features"])) + + +def getmag(dataitem): + magnitude = dataitem["properties"]["mag"] + if (magnitude is None): + magnitude = 0 + return float(magnitude) + + +print(min(data["features"], key=getmag)) +print(max(data["features"], key=getmag)) diff --git a/Start/Ch_1/sorting.py b/Start/Ch_1/sorting.py index 239367b..1813c07 100644 --- a/Start/Ch_1/sorting.py +++ b/Start/Ch_1/sorting.py @@ -8,21 +8,31 @@ names = ["Jeff", "Bill", "Addie", "Stephanie", "Zach", "Lukas", "Joe", "Stacy"] # TODO: the sorted() function can be used to return a new list with sorted data - +# result1 = sorted(numbers) +# print(numbers) +# print(result1) # TODO: alternately, you can use the list object's sort() method, which sorts the list in-place - +# print(names) +# names.sort(reverse=True) +# print(names) # TODO: To sort custom objects, we can tell the sort function which property to use # by specifying a key function # open the data file and load the JSON -# with open("../../30DayQuakes.json", "r") as datafile: -# data = json.load(datafile) +with open("../../30DayQuakes.json", "r") as datafile: + data = json.load(datafile) + + +def getmag(dataitem): + magnitude = dataitem["properties"]["mag"] + if (magnitude is None): + magnitude = 0 + return float(magnitude) -# def getmag(dataitem): -# magnitude = dataitem["properties"]["mag"] -# if (magnitude is None): -# magnitude = 0 -# return float(magnitude) +# Sort on magnitude value, descending order +data["features"].sort(key=getmag, reverse=True) +for i in range(0, 10): + print(data["features"][i]["properties"]["place"]) diff --git a/Start/Ch_1/transform.py b/Start/Ch_1/transform.py index e589516..69d6044 100644 --- a/Start/Ch_1/transform.py +++ b/Start/Ch_1/transform.py @@ -3,6 +3,7 @@ import json import pprint +import datetime def squareFunc(x): @@ -26,20 +27,40 @@ def toGrade(x): grades = (81, 89, 94, 78, 61, 66, 99, 74) # TODO: use map to create a new sequence of values +squares = list(map(squareFunc, nums)) +# print(nums) +# print(squares) # TODO: use sorted and map to change numbers to grades +grades = sorted(grades) +letters = list(map(toGrade, grades)) +print(grades) +print(letters) # Use the filter on our data - let's filter out all seismic events that were *not* quakes # open the data file and load the JSON -# with open("../../30DayQuakes.json", "r") as datafile: -# data = json.load(datafile) - +with open("../../30DayQuakes.json", "r") as datafile: + data = json.load(datafile) # filter the data down to the largest events -# def bigmag(q): -# return q['properties']['mag'] is not None and q['properties']['mag'] >= 6 -# results = list(filter(bigmag, data['features'])) +def bigmag(q): + return q['properties']['mag'] is not None and q['properties']['mag'] >= 6 + + +results = list(filter(bigmag, data["features"])) # TODO: transform the largest events into a simpler structure + + +def simplify(q): + return { + "place": q["properties"]["place"], + "magnitude": q["properties"]["mag"], + "date": str(datetime.date.fromtimestamp(q["properties"]["time"]/1000)) + } + + +results = list(map(simplify, results)) +pprint.pp(results) diff --git a/Start/Ch_1/utility.py b/Start/Ch_1/utility.py index 5ccdbc5..3218a6c 100644 --- a/Start/Ch_1/utility.py +++ b/Start/Ch_1/utility.py @@ -5,25 +5,29 @@ values = [0, 1, 2, 3, 4, 5] # TODO: any() can be used to see if any value in a sequence is True - +# print(any(values)) # TODO: all() will detect if all of the values in a sequence are True - +# print(all(values)) # TODO: sum() can be use to add all of the values in a sequence - +# print(any(values)) # these utility functions don't have callbacks like min or max, # but we can use a generator for more fine control # open the data file and load the JSON -# with open("../../30DayQuakes.json", "r") as datafile: -# data = json.load(datafile) +with open("../../30DayQuakes.json", "r") as datafile: + data = json.load(datafile) # TODO: are there any quake reports that were felt by more than 25,000 people? - +print(any(quake["properties"]["felt"] is not None and quake["properties"]["felt"] > 25000 + for quake in data["features"])) # TODO: how many quakes were felt by more than 500 people? - +print(sum(quake["properties"]["felt"] is not None and quake["properties"]["felt"] > 500 + for quake in data["features"])) # TODO: how many quakes had a magnitude of 6 or larger? +print(sum(quake["properties"]["mag"] is not None and quake["properties"]["mag"] >= 6.0 + for quake in data["features"])) diff --git a/Start/Ch_2/challenge_start.py b/Start/Ch_2/challenge_start.py index 9850216..33dea4d 100644 --- a/Start/Ch_2/challenge_start.py +++ b/Start/Ch_2/challenge_start.py @@ -2,8 +2,17 @@ # Programming challenge: use advanced data collections on the earthquake data import json - +from collections import defaultdict # open the data file and load the JSON with open("../../30DayQuakes.json", "r") as datafile: data = json.load(datafile) + +# Print type of event and the number of events for that type. +totals = defaultdict(int) +for event in data['features']: + totals[event['properties']['type']] +=1 + + +for k,v in totals.items(): + print(f"{k:15}: {v}") \ No newline at end of file diff --git a/Start/Ch_2/counter.py b/Start/Ch_2/counter.py index 81e9d4b..01d0ba3 100644 --- a/Start/Ch_2/counter.py +++ b/Start/Ch_2/counter.py @@ -12,15 +12,25 @@ "Gabby", "Kelly", "James", "Joe", "Sam", "Tara", "Ziggy"] # TODO: Create a Counter for class1 and class2 +c1 = Counter(class1) +c2 = Counter(class2) # TODO: How many students in class 1 named James? +print(c1["James"]) # TODO: How many students are in class 1? +print(sum(c1.values()), " students in class 1") # TODO: Combine the two classes +c1.update(class2) +print(sum(c1.values()), " students in class 1") # TODO: What's the most common name in the two classes? +print(c1.most_common(3)) # TODO: Separate the classes again +c1.subtract(class2) +print(c1.most_common(3)) # TODO: What's common between the two classes? +print(c1 & c2) diff --git a/Start/Ch_2/defaultdict.py b/Start/Ch_2/defaultdict.py index 686d65d..1ea4d5a 100644 --- a/Start/Ch_2/defaultdict.py +++ b/Start/Ch_2/defaultdict.py @@ -8,10 +8,11 @@ 'apple', 'grape', 'banana', 'banana'] # TODO: use a dictionary to count each element -fruitCounter = dict() +fruitCounter = defaultdict(int) # TODO: Count the elements in the list for fruit in fruits: fruitCounter[fruit] += 1 # TODO: print the result +print(fruitCounter) diff --git a/Start/Ch_2/deque.py b/Start/Ch_2/deque.py index 0f827f8..65035ee 100644 --- a/Start/Ch_2/deque.py +++ b/Start/Ch_2/deque.py @@ -4,12 +4,25 @@ import string -# TODO: initialize a deque with lowercase letters +# TODO: initialize a deque with lowercase letters] +d = collections.deque(string.ascii_lowercase) # TODO: deques support the len() function +print(f"Item count: {len(d)}") # TODO: deques can be iterated over +# for elem in d: +# print(elem, elem.upper()) # TODO: manipulate items from either end +d.pop() +d.popleft() +d.append(2) +d.appendleft(1) + # TODO: use an index to get a particular item +print(d) +d.rotate(1) +print(d) +print(d[5]) diff --git a/Start/Ch_2/namedtuple.py b/Start/Ch_2/namedtuple.py index a2c8bcb..c30408b 100644 --- a/Start/Ch_2/namedtuple.py +++ b/Start/Ch_2/namedtuple.py @@ -4,5 +4,13 @@ # TODO: create a Point namedtuple +Point = collections.namedtuple("Point", "x y") +p1 = Point(10, 20) +p2 = Point(30, 40) + +print(p1, p2) +print(p1.x, p1.y) # TODO: use _replace to create a new instance +p1 = p1._replace(x=100) +print(p1.x, p1.y) diff --git a/Start/Ch_3/largequakes.csv b/Start/Ch_3/largequakes.csv new file mode 100644 index 0000000..0b78af3 --- /dev/null +++ b/Start/Ch_3/largequakes.csv @@ -0,0 +1,81 @@ +Place,Magnitude,Link,Date +"15km W of Petrolia, CA",5.21,https://earthquake.usgs.gov/earthquakes/eventpage/nc73355700,2020-03-18 +"246km S of Kangin, Indonesia",6.2,https://earthquake.usgs.gov/earthquakes/eventpage/us60008hzl,2020-03-18 +"6km NNE of Magna, Utah",5.7,https://earthquake.usgs.gov/earthquakes/eventpage/uu60363602,2020-03-18 +"158km WSW of Lata, Solomon Islands",5.5,https://earthquake.usgs.gov/earthquakes/eventpage/us60008hml,2020-03-18 +"97km NNW of Sola, Vanuatu",6.1,https://earthquake.usgs.gov/earthquakes/eventpage/us60008hkg,2020-03-18 +"142km N of Tobelo, Indonesia",5.4,https://earthquake.usgs.gov/earthquakes/eventpage/us60008hkf,2020-03-18 +"172km E of Hihifo, Tonga",6,https://earthquake.usgs.gov/earthquakes/eventpage/us60008h76,2020-03-17 +"53km NNW of Tome, Chile",5.6,https://earthquake.usgs.gov/earthquakes/eventpage/us60008gzt,2020-03-17 +"195km SSE of Lata, Solomon Islands",5.1,https://earthquake.usgs.gov/earthquakes/eventpage/us60008g8y,2020-03-15 +"95km WNW of Bandar 'Abbas, Iran",5.3,https://earthquake.usgs.gov/earthquakes/eventpage/us60008g7m,2020-03-15 +"116km SW of Panenggoede, Indonesia",5.1,https://earthquake.usgs.gov/earthquakes/eventpage/us60008g6f,2020-03-15 +"25km SW of Villa Comaltitlan, Mexico",5.1,https://earthquake.usgs.gov/earthquakes/eventpage/us60008fuy,2020-03-15 +"298km NE of Raoul Island, New Zealand",6.3,https://earthquake.usgs.gov/earthquakes/eventpage/us60008fl8,2020-03-14 +"4km SE of Puerto Armuelles, Panama",5.1,https://earthquake.usgs.gov/earthquakes/eventpage/us60008euj,2020-03-13 +"86km ENE of Iquique, Chile",5.2,https://earthquake.usgs.gov/earthquakes/eventpage/us60008eru,2020-03-13 +"107km WSW of San Nicolas, Philippines",5.2,https://earthquake.usgs.gov/earthquakes/eventpage/us60008epc,2020-03-12 +"26km NNW of Nanao, Japan",5.3,https://earthquake.usgs.gov/earthquakes/eventpage/us60008egj,2020-03-12 +"111km SE of Neiafu, Tonga",5.3,https://earthquake.usgs.gov/earthquakes/eventpage/us60008e9x,2020-03-12 +"99km S of Wonosari, Indonesia",5.1,https://earthquake.usgs.gov/earthquakes/eventpage/us60008e67,2020-03-12 +"112km SE of Neiafu, Tonga",5.3,https://earthquake.usgs.gov/earthquakes/eventpage/us60008dwq,2020-03-11 +"70km S of Panguna, Papua New Guinea",5.3,https://earthquake.usgs.gov/earthquakes/eventpage/us60008dpf,2020-03-11 +"134km SE of L'Esperance Rock, New Zealand",5.3,https://earthquake.usgs.gov/earthquakes/eventpage/us60008dgf,2020-03-11 +"127km SW of Kuripan, Indonesia",5.5,https://earthquake.usgs.gov/earthquakes/eventpage/us60008d51,2020-03-10 +"88km NNE of San Pedro de Atacama, Chile",5.1,https://earthquake.usgs.gov/earthquakes/eventpage/us60008d4v,2020-03-10 +"209km NNW of Puerto Ayora, Ecuador",5.5,https://earthquake.usgs.gov/earthquakes/eventpage/us60008d3z,2020-03-10 +"145km ENE of Luring, China",5.3,https://earthquake.usgs.gov/earthquakes/eventpage/us60008clh,2020-03-09 +"156km NNE of Rumung, Micronesia",5.2,https://earthquake.usgs.gov/earthquakes/eventpage/us60008cdq,2020-03-09 +"69km W of Petrolia, CA",5.77,https://earthquake.usgs.gov/earthquakes/eventpage/nc73351710,2020-03-09 +"30km SSE of Canico, Portugal",5.2,https://earthquake.usgs.gov/earthquakes/eventpage/us60008brw,2020-03-07 +West Chile Rise,5.1,https://earthquake.usgs.gov/earthquakes/eventpage/us60008bmi,2020-03-07 +"294km WNW of Saumlaki, Indonesia",5.2,https://earthquake.usgs.gov/earthquakes/eventpage/us60008blz,2020-03-07 +"53km WSW of Port-Vila, Vanuatu",5.5,https://earthquake.usgs.gov/earthquakes/eventpage/us60008bj6,2020-03-07 +"71km SE of Estacion Coahuila, B.C., MX",5.49,https://earthquake.usgs.gov/earthquakes/eventpage/ci38385946,2020-03-07 +"23km ESE of Buenos Aires, Costa Rica",5.2,https://earthquake.usgs.gov/earthquakes/eventpage/us60008bgr,2020-03-07 +Southwest Indian Ridge,5.9,https://earthquake.usgs.gov/earthquakes/eventpage/us60008ay3,2020-03-06 +Southern Mid-Atlantic Ridge,5.4,https://earthquake.usgs.gov/earthquakes/eventpage/us60008a9i,2020-03-05 +"168km ESE of Raoul Island, New Zealand",5.6,https://earthquake.usgs.gov/earthquakes/eventpage/us60008a7b,2020-03-05 +"161km ENE of Angoram, Papua New Guinea",5.2,https://earthquake.usgs.gov/earthquakes/eventpage/us60008a39,2020-03-05 +"39km S of Tinogasta, Argentina",5.5,https://earthquake.usgs.gov/earthquakes/eventpage/us600089zt,2020-03-05 +"67km S of Bristol Island, South Sandwich Islands",5.1,https://earthquake.usgs.gov/earthquakes/eventpage/us600089h2,2020-03-04 +"50km W of Amatignak Island, Alaska",5.1,https://earthquake.usgs.gov/earthquakes/eventpage/us600088i8,2020-03-03 +"64km S of Bristol Island, South Sandwich Islands",5.1,https://earthquake.usgs.gov/earthquakes/eventpage/us6000885x,2020-03-02 +"53km WSW of Amatignak Island, Alaska",5.7,https://earthquake.usgs.gov/earthquakes/eventpage/us600087lz,2020-03-02 +"5km NNE of Culasian, Philippines",5.3,https://earthquake.usgs.gov/earthquakes/eventpage/us600087il,2020-03-01 +"158km N of Tobelo, Indonesia",5.3,https://earthquake.usgs.gov/earthquakes/eventpage/us600086um,2020-02-29 +"135km ESE of Tadine, New Caledonia",5.1,https://earthquake.usgs.gov/earthquakes/eventpage/us600086su,2020-02-29 +"103km SE of Neiafu, Tonga",5.3,https://earthquake.usgs.gov/earthquakes/eventpage/us600086p0,2020-02-29 +"201km NW of Visokoi Island, South Georgia and the South Sandwich Islands",5.1,https://earthquake.usgs.gov/earthquakes/eventpage/us600086as,2020-02-28 +"43km WSW of Pagaralam, Indonesia",5.1,https://earthquake.usgs.gov/earthquakes/eventpage/us6000863e,2020-02-28 +"117km ESE of Neiafu, Tonga",5.7,https://earthquake.usgs.gov/earthquakes/eventpage/us600085x5,2020-02-28 +Southern East Pacific Rise,5.4,https://earthquake.usgs.gov/earthquakes/eventpage/us600085my,2020-02-27 +"179km S of Sarangani, Philippines",5.5,https://earthquake.usgs.gov/earthquakes/eventpage/us600085g3,2020-02-27 +Southern Mid-Atlantic Ridge,5.3,https://earthquake.usgs.gov/earthquakes/eventpage/us6000859r,2020-02-27 +Southern Mid-Atlantic Ridge,5.1,https://earthquake.usgs.gov/earthquakes/eventpage/us60008535,2020-02-27 +"48km NNW of Saumlaki, Indonesia",5.9,https://earthquake.usgs.gov/earthquakes/eventpage/us600084gu,2020-02-26 +"109km WNW of Kirakira, Solomon Islands",5.2,https://earthquake.usgs.gov/earthquakes/eventpage/us6000848g,2020-02-25 +"50km NNW of Hualian, Taiwan",5.1,https://earthquake.usgs.gov/earthquakes/eventpage/us70007we1,2020-02-25 +"79km W of Santiago de Cao, Peru",5.2,https://earthquake.usgs.gov/earthquakes/eventpage/us70007w9l,2020-02-25 +"240km SE of Lambasa, Fiji",5.2,https://earthquake.usgs.gov/earthquakes/eventpage/us70007w8g,2020-02-25 +"83km ESE of Muara Siberut, Indonesia",5.2,https://earthquake.usgs.gov/earthquakes/eventpage/us70007w28,2020-02-24 +"24km SE of Saray, Turkey",6,https://earthquake.usgs.gov/earthquakes/eventpage/us70007v9g,2020-02-23 +"67km NE of Kuril'sk, Russia",5.2,https://earthquake.usgs.gov/earthquakes/eventpage/us70007v6u,2020-02-23 +"26km ESE of Saray, Turkey",5.8,https://earthquake.usgs.gov/earthquakes/eventpage/us70007v29,2020-02-23 +"141km SE of Modayag, Indonesia",5.1,https://earthquake.usgs.gov/earthquakes/eventpage/us70007v12,2020-02-23 +"135km SE of Modayag, Indonesia",5.3,https://earthquake.usgs.gov/earthquakes/eventpage/us70007v0d,2020-02-23 +West Chile Rise,5.5,https://earthquake.usgs.gov/earthquakes/eventpage/us70007uy4,2020-02-22 +"62km SSE of `Ohonua, Tonga",5.5,https://earthquake.usgs.gov/earthquakes/eventpage/us70007uvu,2020-02-22 +"40km SSE of `Ohonua, Tonga",5.2,https://earthquake.usgs.gov/earthquakes/eventpage/us70007uuu,2020-02-22 +"110km SSE of Pondaguitan, Philippines",5.2,https://earthquake.usgs.gov/earthquakes/eventpage/us70007usp,2020-02-22 +"114km SSE of Pondaguitan, Philippines",5.1,https://earthquake.usgs.gov/earthquakes/eventpage/us70007usm,2020-02-22 +"144km E of Chichi-shima, Japan",5.2,https://earthquake.usgs.gov/earthquakes/eventpage/us70007upz,2020-02-22 +"36km SSW of Amatignak Island, Alaska",5.2,https://earthquake.usgs.gov/earthquakes/eventpage/us70007u2w,2020-02-21 +"229km NW of Saumlaki, Indonesia",5.4,https://earthquake.usgs.gov/earthquakes/eventpage/us70007tt3,2020-02-20 +"153km ENE of Petropavlovsk-Kamchatskiy, Russia",5.8,https://earthquake.usgs.gov/earthquakes/eventpage/us70007trb,2020-02-20 +"286km NNE of Luring, China",5.1,https://earthquake.usgs.gov/earthquakes/eventpage/us70007tnv,2020-02-20 +"13km SE of Kalbay, Philippines",5.4,https://earthquake.usgs.gov/earthquakes/eventpage/us70007th0,2020-02-20 +South of the Kermadec Islands,5.1,https://earthquake.usgs.gov/earthquakes/eventpage/us70007tf0,2020-02-20 +"118km SE of Visokoi Island, South Georgia and the South Sandwich Islands",5.1,https://earthquake.usgs.gov/earthquakes/eventpage/us70007tdi,2020-02-20 +"93km SSW of Merizo Village, Guam",5.1,https://earthquake.usgs.gov/earthquakes/eventpage/us70007t2i,2020-02-19 +North of Svalbard,5.2,https://earthquake.usgs.gov/earthquakes/eventpage/us70007s90,2020-02-18 diff --git a/Start/Ch_3/serialize_csv.py b/Start/Ch_3/serialize_csv.py index 3486d15..acacfaa 100644 --- a/Start/Ch_3/serialize_csv.py +++ b/Start/Ch_3/serialize_csv.py @@ -19,7 +19,21 @@ def isbig(x): largequakes = list(filter(isbig, data["features"])) # TODO: Create the header and row structures for the data +header = ["Place", "Magnitude", "Link", "Date"] # TODO: populate the rows with the resulting quake data +rows = [] +for quake in largequakes: + thedate = datetime.date.fromtimestamp( + int(quake["properties"]["time"] / 1000)) + rows.append([quake["properties"]["place"], + quake["properties"]["mag"], + quake["properties"]["url"], + thedate]) + # TODO: write the results to the CSV file +with open("largequakes.csv", "w") as csvfile: + writer = csv.writer(csvfile, delimiter=",") + writer.writerow(header) + writer.writerows(rows)