-
Notifications
You must be signed in to change notification settings - Fork 16
/
csv2js.py
38 lines (33 loc) · 806 Bytes
/
csv2js.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
'''
convert csv file to json objects
usage: cat csv_file | python csv2js.py > json_file
note:
1) 1st line must be header
2) also remember to remove label from 'features' so it's not used for training
3) then drag and drop to the drop zone of learningjs page
'''
import sys
header=True
h=[]
data=[]
print 'var trainData=['
lastfs=None
for l in sys.stdin:
if(header):
fs = l.strip().split(',')
h = fs
header=False
else:
fs = l.strip().split(',')
if lastfs!=None:
print '{',
print ','.join([h[idx]+':\''+a+'\'' for idx,a in enumerate(lastfs)]),
print '},'
lastfs = fs
print '{',
print ','.join([h[idx]+':\''+a+'\'' for idx,a in enumerate(lastfs)]),
print '}'
print ']'
print 'var features=['
print ','.join(['\''+a+'\'' for a in h])
print ']'