-
Notifications
You must be signed in to change notification settings - Fork 1
/
csvimport.py
43 lines (38 loc) · 1.08 KB
/
csvimport.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
import csv
import difflib
import string
import pymongo
from pymongo import MongoClient
def getCurrentData():
client = MongoClient()
db = client.test
collection = db.planets
with open('exoplanets.csv', 'rU') as csvfile:
dataReader = csv.reader(csvfile)
columns = [];
for dataRow in dataReader:
if(len(columns) == 0):
columns = columns + dataRow
else:
planet = {
"mass" : toFloat(dataRow[columns.index('pl_masse')]),
"orbital_period": toFloat(dataRow[columns.index('pl_orbper')]),
"radius": toFloat(dataRow[columns.index('pl_rade')]),
"year_discovered": toInt(dataRow[columns.index('pl_disc')]),
"dist_from_earth": toFloat(dataRow[columns.index('st_dist')]),
"temp": toFloat(dataRow[columns.index('st_teff')]),
"name": dataRow[columns.index('pl_name')],
"neighbors": toInt(dataRow[columns.index('pl_pnum')])
}
collection.insert(planet)
def toFloat(s):
try:
return float(s)
except ValueError:
return None
def toInt(s):
try:
return int(s)
except ValueError:
return None
getCurrentData()