forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjson_to_csv.python
More file actions
executable file
·51 lines (45 loc) · 1.5 KB
/
json_to_csv.python
File metadata and controls
executable file
·51 lines (45 loc) · 1.5 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
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
import sys, json, csv, string
if len(sys.argv) != 3:
sys.exit("usage: json2csv.py input.json output.csv")
try:
data = json.load(open(sys.argv[1]))
except Exception:
sys.exit("invalid json")
if set(data) != {"CORE", "LB", "PAR"}:
sys.exit("invalid categories")
arches = []
seen = set()
for cat in data.values():
if not isinstance(cat, dict):
sys.exit("data not 2-dimensional")
for param in cat.values():
if not isinstance(param, dict):
sys.exit("data not 2-dimensional")
for a in param.keys():
if a not in seen:
seen.add(a)
arches.append(a)
cols = 1 + len(arches)
empty = [""] * cols
arches = sorted(arches, key=lambda x: 0 if x.startswith("default") else 1)
with open(sys.argv[2], "w", newline="") as f:
w = csv.writer(f, lineterminator="\n")
w.writerow(["Architecture", *arches])
w.writerow(empty)
cats = list(data.items())
for ci, (cname, cat) in enumerate(cats):
w.writerow([f"{cname}:"] + [""] * (cols - 1))
for pname, param in cat.items():
row = [pname]
for a in arches:
v = param.get(a, "")
if isinstance(v, list):
row.append(json.dumps(v))
elif isinstance(v, str) and not v == "":
row.append('"' + v + '"')
else:
row.append(v)
w.writerow(row)
if ci != len(cats) - 1:
w.writerow(empty)