-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite.py
47 lines (43 loc) · 1.66 KB
/
write.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""Write Results to Json or CSV."""
import csv
import json
from models import NearEarthObject
import helpers
def write_to_csv(results, filename):
"""Write results to CSV."""
fieldnames = ('datetime_utc',
'distance_au',
'velocity_km_s',
'designation',
'name',
'diameter_km',
'potentially_hazardous')
with open(filename, 'w') as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
for approach in results:
line = {}
line['datetime_utc'] = helpers.datetime_to_str(approach.time)
line['distance_au'] = approach.distance
line['velocity_km_s'] = approach.velocity
line['designation'] = approach._designation
line['name'] = approach.neo.fullname
line['diameter_km'] = approach.neo.diameter
line['potentially_hazardous'] = approach.neo.hazardous
writer.writerow(line)
def write_to_json(results, filename):
"""Write Results to Json."""
output = []
for approach in results:
item = {'datetime_utc': approach.time_str,
'distance_au': approach.distance,
'velocity_km_s': approach.velocity,
'neo': {'designation': approach._designation,
'name': approach.neo.name,
'diameter_km': approach.neo.diameter,
'potentially_hazardous': approach.neo.hazardous
}
}
output.append(item)
with open(filename, 'w') as outfile:
json.dump(output, outfile)