/
csv2json.py
60 lines (51 loc) · 1.69 KB
/
csv2json.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
import csv
import sys, getopt
def main(argv):
inputfile = ''
outputfile = ''
try:
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
except getopt.GetoptError:
print 'test.py -i <inputfile> -o <outputfile>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'test.py -i <inputfile> -o <outputfile>'
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
print 'Input file is "', inputfile
print 'Output file is "', outputfile
jsfile = file(inputfile+'.json', 'w')
jsfile.write('[\r\n')
with open(inputfile,'r') as f:
next(f) # skip headings
reader=csv.reader(f,delimiter='\t')
# get the total number of rows excluded the heading
row_count = len(list(reader))
ite = 0
# back to first position
f.seek(0)
next(f) # skip headings
for id,identifier,generation_id,is_main_series in reader:
ite+= 1
jsfile.write('\t{\r\n')
n = '\t\t\"id\": \"' + id + '\",\r\n'
i = '\t\t\"identifier\": \"' + identifier + '\",\r\n'
d = '\t\t\"generation_id\": \"' + generation_id + '\"\r\n'
e = '\t\t\"is_main_series\": \"' + is_main_series + '\"\r\n'
jsfile.write(n)
jsfile.write(i)
jsfile.write(d)
jsfile.write(e)
jsfile.write('\t}')
# omit comma for last row item
if ite < row_count:
jsfile.write(',\r\n')
jsfile.write('\r\n')
jsfile.write(']')
jsfile.close()
if __name__ == '__main__':
main(sys.argv[1:])