-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
89 lines (83 loc) · 2.98 KB
/
main.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import re
import sys
path = sys.argv[1] # Gets path of .csv file to be converted
csvFile = open(path, "r", encoding="utf-8") # Open the .csv file for reading
csvLines = csvFile.readlines() # Read the lines of .csv file
test = re.compile(r'(?:[^,{.*}]|\{[^)]*\})+')
headers = test.findall(csvLines.pop(0))[:-1]
listType = []
header_num = 0
for header in headers:
if header == '':
listType.append(None)
val = re.search(r'\{(.*?)\}', header)
if val == None:
listType.append(val)
else:
func = re.search(r'\:\:(.*)', header)
if func == None:
listType.append((val.groups()[0], None))
else:
listType.append((val.groups()[0], func.groups()[0]))
headerName = headers[header_num] if listType[header_num] == None else re.search(r'(.*)\{', headers[header_num]).groups()[0]
headers[header_num] = re.sub(headerName, headerName + '_' + func.groups()[0], headers[header_num])
header_num += 1
# Functions allowed as of the moment of implementation: max, min, media, sum
def applyFunction(func, l):
n = len(l)
res = 0
if func == "sum":
for i in l:
res += i
elif func == "media":
for i in l:
res += i
res /= n
elif func == "max":
res = max(l)
elif func == "min":
res = min(l)
else:
res = -1
return res
values = []
for line in csvLines:
cols = re.split(r',|\n', line)
cols.pop()
value = []
nToSkip = 0
for i in range(len(headers)):
if nToSkip != 0:
nToSkip -= 1
pass
if listType[i] == None:
value.append(cols[i])
else:
listSize = re.split(r',', listType[i][0])
rang = listSize[0] if len(listSize) == 1 else listSize[1]
nToSkip = rang
lista = []
for j in range(int(rang)):
val = cols[i + j]
if val != '':
lista.append(int(val))
useFunc = listType[i][1]
if useFunc != None:
value.append(applyFunction(useFunc, lista))
else:
value.append(lista)
values.append(value)
with open(re.sub(r'\.csv', ".json", path), "w", encoding="utf-8") as jsonFile:
if len(values) == 0:
jsonFile.write("[]\n")
else:
jsonFile.write("[\n")
num = 0
for val in values:
jsonFile.write("\t{\n")
for i in range(len(val)):
name = headers[i] if listType[i] == None else re.search(r'(.*)\{', headers[i]).groups()[0]
jsonFile.write("\t\t\"{}\": {}{}\n".format(name, val[i] if type(val[i]) == list or type(val[i]) == int or type(val[i]) == float or val[i].isnumeric() else "\"{}\"".format(val[i]), "" if i == (len(val) - 1) else ","))
jsonFile.write("\t{}\n".format("}" if num + 1 == len(values) else "},"))
num += 1
jsonFile.write("]\n")