-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcsvtojson.py
More file actions
44 lines (36 loc) · 981 Bytes
/
csvtojson.py
File metadata and controls
44 lines (36 loc) · 981 Bytes
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
#set up paths and vars
import sys
if len(sys.argv) == 3:
csvfile = open(sys.argv[1],'r')
jsonfile = open(sys.argv[2], 'w')
elif len(sys.argv) == 2:
csvfile = open(sys.argv[1],'r')
jsonfile = open('output.json', 'w')
else:
print "Usage - csvtojson.py inputfile.csv output.json"
sys.exit()
arr=[]
headers = []
# Read in the headers/first row
for header in csvfile.readline().split(','):
headers.append(header)
# Extract the information into the "xx" : "yy" format.
for line in csvfile.readlines():
lineStr = ''
for i,item in enumerate(line.split(',')):
lineStr+='"'+headers[i] +'" : "' + item + '",\n'
arr.append(lineStr)
csvfile.close()
#convert the array into a JSON string:
jsn = '{\n "data":['
jsnEnd = ']\n}'
for i in range(len(arr)-1):
if i == len(arr)-2:
jsn+="{"+str(arr[i])[:-2]+"}\n"
else:
jsn+="{"+str(arr[i])[:-2]+"},\n"
jsn+=jsnEnd
#write to file
jsonfile.write(jsn)
jsonfile.close()
print "Done."