-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonGenerator.py
More file actions
138 lines (103 loc) · 4.47 KB
/
Copy pathJsonGenerator.py
File metadata and controls
138 lines (103 loc) · 4.47 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# Author: Anderrow
# JSON Generator using Polars
# https://github.com/anderrow
import glob
import json
import os
import re
import time
from pathlib import Path
import polars as pl
start_time = time.perf_counter()
SCRIPT_DIR = Path(__file__).resolve().parent
ROOT_DIR = SCRIPT_DIR.parent
source_folder = Path(
os.environ.get("JSON_GENERATOR_SOURCE_DIR", ROOT_DIR / "SOURCE FILES")
).resolve()
json_root = Path(
os.environ.get("JSON_GENERATOR_OUTPUT_DIR", ROOT_DIR / "JSON FILES")
).resolve()
excel_files = glob.glob(os.path.join(str(source_folder), "*.csv"))
dfs = {}
def get_exportable_columns(df, sheet_name):
export_columns = []
skipped_columns = 0
for column_name in df.columns[1:]:
normalized_name = "" if column_name is None else str(column_name).strip()
if not normalized_name or re.fullmatch(r"_duplicated_\d+", normalized_name):
skipped_columns += 1
continue
export_columns.append(column_name)
if skipped_columns:
print(
f"Warning: skipping {skipped_columns} empty header column(s) in {sheet_name}."
)
return export_columns
# -------------------------- READ ALL CSV FILES -------------------------- #
for path in excel_files:
sheet_name = os.path.splitext(os.path.basename(path))[0]
try:
df = pl.read_csv(path)
if df.height == 0 or df.columns is None or any(col is None for col in df.columns):
print(f"Warning: file '{sheet_name}' has invalid or missing headers, skipping...")
continue
dfs[sheet_name] = df
print(
f"File '{sheet_name}' read successfully with {df.height} rows and {df.width} columns."
)
except Exception as e:
print(f"Warning: error processing sheet '{sheet_name}': {e}")
print(f"current time {time.perf_counter() - start_time:.2f} seconds")
# ---------------------------- FILTER DATAFRAMES ---------------------------- #
df_filtered_dict = {}
for sheet_name, df in dfs.items():
df = df.with_columns(pl.col(df.columns[2]).fill_null("").cast(pl.Utf8))
df_filtered = df.filter(~pl.col(df.columns[2]).str.contains(r"(?i).*Undefined"))
df_filtered2 = df_filtered.filter(
~pl.col(df.columns[2]).str.contains(r"(?i)Unkown message")
)
df_filtered_subset = df_filtered2.select(df_filtered2.columns[1:9])
df_cleaned = df_filtered_subset.filter(pl.col(df_filtered_subset.columns[1]) != "")
df_filtered_dict[sheet_name] = df_cleaned
print("=" * 50)
print(f"Sheet: {sheet_name}")
print(f"Original length: {len(df)}")
print(f"Filtered length: {len(df_cleaned)}")
print(f"Data has been reduced by a {100 - len(df_cleaned) * 100 / len(df):.2f}%")
print("=" * 50)
# -------------------------- VALIDATE COLUMN NAMES -------------------------- #
column_name_pattern = r"^[a-z]{2}-[A-Z]{2}$"
export_columns_by_sheet = {}
for sheet_name, df in df_filtered_dict.items():
export_columns = get_exportable_columns(df, sheet_name)
export_columns_by_sheet[sheet_name] = export_columns
for column_name in export_columns:
if not re.match(column_name_pattern, column_name):
raise ValueError(
f"\nColumn name '{column_name}' of {sheet_name} does not match format [a-z][a-z]-[A-Z][A-Z]"
)
# --------------------------- GENERATE JSON FILES --------------------------- #
for sheet_name, df in df_filtered_dict.items():
output_folder = json_root / sheet_name
os.makedirs(output_folder, exist_ok=True)
export_columns = export_columns_by_sheet.get(sheet_name, [])
if not export_columns:
print(f"\nSkipping {sheet_name}: no exportable columns found.")
continue
print(f"\nFolder: {sheet_name}")
keys = df.select(df.columns[0]).to_series().to_list()
last_json_filename = None
for column_name in export_columns:
column_name_simple = re.sub(r"^([a-zA-Z]+)-.*", r"\1", column_name)
json_filename = output_folder / f"{column_name_simple}.json"
values = df.select(column_name).to_series().to_list()
df_to_json = dict(zip(keys, values))
with open(json_filename, "w", encoding="utf-8") as json_file:
json.dump(df_to_json, json_file)
last_json_filename = json_filename
print(f"JSON file created: {json_filename.as_posix()}")
if last_json_filename is not None:
print(f"Completed: {sheet_name}")
end_time = time.perf_counter()
elapsed = end_time - start_time
print(f"\nTotal execution time: {elapsed:.2f} seconds")