-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path52_csv2json.py
More file actions
52 lines (39 loc) · 933 Bytes
/
52_csv2json.py
File metadata and controls
52 lines (39 loc) · 933 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
45
46
47
48
49
50
51
52
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# = = = [ EXECUTION EXAMPLE ] = = = ] #
#
# python3 52_csv2json.py input:"stats_file.csv"
#
# = = = [ / EXECUTION EXAMPLE ] = = = ] #
# import time
import json as jsonold
import sys
FILE = "stats.csv"
if len(sys.argv)>1 and sys.argv[1]!=None: FILE = sys.argv[1]
import csv
D = {}
headers = []
c = 0
with open( FILE , 'r') as f:
reader = csv.reader(f, delimiter=' ')
for row in reader:
if c==0:
headers = row
c += 1
else:
D[ row[0] ] = {}
k = 6
for e in range(len(row)):
try:
# print( headers[e] , "->" , row[e] )
D[ row[0] ][ headers[e] ] = row[e]
except:
if "k" not in D[ row[0] ]:
D[ row[0] ][ "k" ] = {}
D[ row[0] ][ "k" ][k] = row[e].split("*")
# print( "\t" , "k =",k ,"->", row[e].split("*") )
k += 1
pass
f = open( FILE.replace(".csv",".json") , "w" )
f.write( jsonold.dumps( D ) )
f.close()