Desynth page and improved item info api. Added string substitution to utils.
This commit is contained in:
78
scripts/convert_hq_entries.py
Normal file
78
scripts/convert_hq_entries.py
Normal file
@@ -0,0 +1,78 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def convert_hq_entries(csv_path: Path, backup: bool = True) -> None:
|
||||
"""Convert HQ1:,HQ2:,HQ3: columns in the desynthesis recipes CSV into a
|
||||
single JSON-like dictionary string stored in the HQ column.
|
||||
|
||||
The CSV currently looks like::
|
||||
Item,Crystal,Ingredients,HQ,Cap
|
||||
Distilled Water x3,Lightning,Tahrongi Cactus,HQ1: Distilled Water x6,HQ2: Distilled Water x9,HQ3: Distilled Waterx12,2
|
||||
|
||||
After conversion it will be::
|
||||
Distilled Water x3,Lightning,Tahrongi Cactus,{"HQ1":"Distilled Water x6","HQ2":"Distilled Water x9","HQ3":"Distilled Waterx12"},2
|
||||
"""
|
||||
csv_path = Path(csv_path)
|
||||
if not csv_path.exists():
|
||||
raise FileNotFoundError(csv_path)
|
||||
|
||||
text = csv_path.read_text(encoding="utf-8").splitlines()
|
||||
output_lines = []
|
||||
|
||||
for line in text:
|
||||
# Keep skill category lines (e.g., "Alchemy") unchanged.
|
||||
if "," not in line:
|
||||
output_lines.append(line)
|
||||
continue
|
||||
|
||||
parts = [p.strip() for p in line.split(",")]
|
||||
|
||||
# The header line already has correct length (5).
|
||||
if parts[:5] == ["Item", "Crystal", "Ingredients", "HQ", "Cap"]:
|
||||
output_lines.append(",".join(parts))
|
||||
continue
|
||||
|
||||
# If this row already has 5 columns, leave as-is.
|
||||
if len(parts) <= 5:
|
||||
output_lines.append(",".join(parts))
|
||||
continue
|
||||
|
||||
# Otherwise consolidate HQ columns.
|
||||
item, crystal, ingredients = parts[:3]
|
||||
cap = parts[-1]
|
||||
hq_parts = parts[3:-1]
|
||||
|
||||
hq_dict = {}
|
||||
unnamed_counter = 1
|
||||
for h in hq_parts:
|
||||
h = h.strip()
|
||||
if not h:
|
||||
continue
|
||||
if ":" in h:
|
||||
key, value = h.split(":", 1)
|
||||
hq_dict[key.strip()] = value.strip()
|
||||
else:
|
||||
# Handle unlabeled HQ values by assigning sequential keys.
|
||||
key = f"HQ{unnamed_counter}"
|
||||
unnamed_counter += 1
|
||||
hq_dict[key] = h
|
||||
|
||||
# Build dictionary string with spaces after commas to match example formatting.
|
||||
hq_json_readable = "{" + ",".join([f'"{k}": "{v}"' if "\"" not in v else f'"{k}": {json.dumps(v)}' for k, v in hq_dict.items()]) + "}"
|
||||
|
||||
new_line = ",".join([item, crystal, ingredients, hq_json_readable, cap])
|
||||
output_lines.append(new_line)
|
||||
|
||||
# Backup original file.
|
||||
if backup:
|
||||
backup_path = csv_path.with_suffix(csv_path.suffix + ".bak")
|
||||
if not backup_path.exists():
|
||||
backup_path.write_text("\n".join(text), encoding="utf-8")
|
||||
|
||||
csv_path.write_text("\n".join(output_lines) + "\n", encoding="utf-8")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
target = Path(__file__).resolve().parents[1] / "datasets" / "desythesis_recipes.csv"
|
||||
convert_hq_entries(target)
|
||||
Reference in New Issue
Block a user