-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv2json4mongo.py
34 lines (33 loc) · 1.19 KB
/
csv2json4mongo.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
import csv
def csv2json4mongo(inputCSV='sample.csv', mongoDocuments='sample.mongo', xField='Lon', yField='Lat'):
try:
csvFile = csv.reader(open(inputCSV, 'r'), delimiter = ',', quotechar = '"')
jsonDocsFile = open(mongoDocuments, 'w')
except:
print 'Could not read input file.'
exit()
is_header = True
is_firstRow = True
for row in csvFile:
geoPoint = ""
if(is_header):
header = row
is_header = False
else:
if is_firstRow == False:
jsonDocsFile.write('\n')
jsonDocsFile.write('{')
column = 0
while column < len(header):
if header[column] == xField:
geoPoint += "[" + str(row[column]) + ","
if header[column] == yField:
geoPoint += str(row[column]) + "]"
jsonDocsFile.write('"' + header[column] + '":"' + row[column] + '"')
jsonDocsFile.write(', ')
column += 1
jsonDocsFile.write('"MongoPoint": ' + geoPoint)
jsonDocsFile.write("}")
is_firstRow = False
jsonDocsFile.write('\n')
return True