Update Exploring Near Earth Objects project and add Meme Generator project
This commit is contained in:
@@ -10,6 +10,7 @@ extension determines which of these functions is used.
|
||||
|
||||
You'll edit this file in Part 4.
|
||||
"""
|
||||
|
||||
import csv
|
||||
import json
|
||||
|
||||
@@ -25,10 +26,24 @@ def write_to_csv(results, filename):
|
||||
:param filename: A Path-like object pointing to where the data should be saved.
|
||||
"""
|
||||
fieldnames = (
|
||||
'datetime_utc', 'distance_au', 'velocity_km_s',
|
||||
'designation', 'name', 'diameter_km', 'potentially_hazardous'
|
||||
"datetime_utc",
|
||||
"distance_au",
|
||||
"velocity_km_s",
|
||||
"designation",
|
||||
"name",
|
||||
"diameter_km",
|
||||
"potentially_hazardous",
|
||||
)
|
||||
# TODO: Write the results to a CSV file, following the specification in the instructions.
|
||||
with open(filename, "w") as csv_file:
|
||||
writer = csv.DictWriter(csv_file, fieldnames)
|
||||
writer.writeheader()
|
||||
for result in results:
|
||||
row = {**result.serialize(), **result.neo.serialize()}
|
||||
row["name"] = "" if row["name"] is None else row["name"]
|
||||
row["diameter_km"] = (
|
||||
"" if row["diameter_km"] is float("nan") else row["diameter_km"]
|
||||
)
|
||||
writer.writerow(row)
|
||||
|
||||
|
||||
def write_to_json(results, filename):
|
||||
@@ -42,4 +57,25 @@ def write_to_json(results, filename):
|
||||
:param results: An iterable of `CloseApproach` objects.
|
||||
:param filename: A Path-like object pointing to where the data should be saved.
|
||||
"""
|
||||
# TODO: Write the results to a JSON file, following the specification in the instructions.
|
||||
data = []
|
||||
for result in results:
|
||||
item = {**result.serialize(), **result.neo.serialize()}
|
||||
item["name"] = "" if item["name"] is None else item["name"]
|
||||
item["diameter_km"] = (
|
||||
"" if item["diameter_km"] is float("nan") else item["diameter_km"]
|
||||
)
|
||||
data.append(
|
||||
{
|
||||
"datetime_utc": item["datetime_utc"],
|
||||
"distance_au": item["distance_au"],
|
||||
"velocity_km_s": item["velocity_km_s"],
|
||||
"neo": {
|
||||
"designation": item["designation"],
|
||||
"name": item["name"],
|
||||
"diameter_km": item["diameter_km"],
|
||||
"potentially_hazardous": item["potentially_hazardous"],
|
||||
},
|
||||
}
|
||||
)
|
||||
with open(filename, "w") as json_file:
|
||||
json.dump(data, json_file, indent=2)
|
||||
|
||||
Reference in New Issue
Block a user