-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
40 lines (34 loc) · 1.43 KB
/
main.py
File metadata and controls
40 lines (34 loc) · 1.43 KB
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
import csv
import json
import os
def transform_json_to_csv():
path = os.getcwd()
inputfile = path + "/" + "res.json"
outputfile = path + "/" + "res.csv"
fieldheader = ["Instance", "BestKnown", "BestExchange", "GapBE", "BestRelocate", "GapBR", "FirstExchange", "GapFE", "FirstRelocate", "GapFR"]
with open(inputfile, mode='r') as jsonfile, open(outputfile, mode="a") as csvfile:
lines = json.load(jsonfile)
csvwriter = csv.writer(csvfile, delimiter=',',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
csvwriter.writerow(fieldheader)
for instance, methods in sorted(lines.iteritems()):
tab = []
perfeccost = -1
for method in methods:
gap = 0.0
ourcost = 0.0
for md, val in method.iteritems():
print md, val
if md == "perfect_cost":
perfeccost = float(val)
if md == "gap":
ourcost = ((float(str(val).replace(" %", "")) / 100) * perfeccost) + perfeccost
ourcost = round(ourcost, 2)
gap = val
tab.append(ourcost)
tab.append(gap)
# writeHere
tab0 = [instance, perfeccost]
csvwriter.writerow(tab0 + tab)
if __name__ == '__main__':
transform_json_to_csv()