forked from eirikmun/xwing-data2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.py
More file actions
59 lines (46 loc) · 1.97 KB
/
init.py
File metadata and controls
59 lines (46 loc) · 1.97 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
52
53
54
55
56
57
58
59
import json
import csv
from pathlib import Path
# Base "pilots" directory
BASE_DIR = Path(r"C:\Users\gregk\Documents\GitHub\xwing-data2\data\pilots")
def main():
rows = []
# Go through each immediate subfolder (7 factions)
for subdir in sorted(p for p in BASE_DIR.iterdir() if p.is_dir()):
# counts[0] = initiative 1, ..., counts[5] = initiative 6
counts = [0] * 6
# Track unique (name, initiative) pairs per folder
seen_name_init = set()
# Look at every JSON file in this folder
for json_file in subdir.glob("*.json"):
try:
with open(json_file, "r", encoding="utf-8") as f:
data = json.load(f)
except Exception as e:
print(f"Error reading {json_file}: {e}")
continue
# Walk through pilots and count initiatives 1–6, deduped by (name, initiative)
for pilot in data.get("pilots", []):
name = pilot.get("name")
init = pilot.get("initiative")
if not name:
continue
if not isinstance(init, int) or not (1 <= init <= 6):
continue
key = (name, init)
if key in seen_name_init:
# Already counted this name+initiative combo in this folder
continue
seen_name_init.add(key)
counts[init - 1] += 1
# Add row: folder name, then counts for initiatives 1..6
rows.append([subdir.name, *counts])
# Output CSV in the same pilots folder
out_path = BASE_DIR / "pilot_initiatives_by_folder_deduped.csv"
with open(out_path, "w", newline="", encoding="utf-8") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["Folder", "Init 1", "Init 2", "Init 3", "Init 4", "Init 5", "Init 6"])
writer.writerows(rows)
print(f"Wrote CSV to: {out_path}")
if __name__ == "__main__":
main()