-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataconverter.py
61 lines (50 loc) · 1.49 KB
/
dataconverter.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Simple Python program that converts CSV to JSON for my MongoDB project.
# - It converts all the csv files under the directory at once.
import csv
import json
import glob
for files in glob.glob("*.csv"):
csvfile = open(files, 'r')
jsonfile = open(files[:-4] + '.json', 'w')
reader = csv.reader(open(files, 'rU'))
fieldnames = ()
out = 0
for row in reader:
temp = []
# print row
fieldnames = row
break
counter = 0
out = ""
for row in reader:
if counter == 0:
# skip
print "0"
else:
# print row
itemCounter = 0
temp = "{"
for item in row:
a = item
# print a
nameCounter = 0
for item in fieldnames:
if itemCounter == nameCounter:
# print item
temp += ' "'
temp += item
temp += '"'
temp += ': "'
temp += a
temp += '",'
nameCounter += 1
itemCounter += 1
temp = temp[:-1]
temp += "}"
# print temp
out += temp
out += ", "
counter += 1
out = out[:-1]
jsonfile.write(out)
print "End of Execution"