Загрузка данных


import csv
import json


def process_treasures(csv_filename, json_filename, secret_key, grouping):
    a = {
        "Предмет": "Item",
        "Количество": "Quantity",
        "Цена_в_золотых": "Price_in_gold",
        "Где_найдено": "Found_in"
    }
    b = a.get(grouping, grouping)
    c = []

    with open(csv_filename, "r", encoding="utf-8", newline="") as d:
        e = csv.DictReader(d, delimiter=".")

        for f in e:
            f["Quantity"] = int(f["Quantity"])
            f["Price_in_gold"] = int(f["Price_in_gold"])
            c.append(f)

    if secret_key != sum(f["Quantity"] for f in c):
        d = {"error": "access denied"}
    else:
        e = {}

        for f in c:
            g = f[b]

            if g not in e:
                e[g] = {
                    "total_items": 0,
                    "total_value": 0,
                    "items": []
                }

            e[g]["total_items"] += f["Quantity"]
            e[g]["total_value"] += f["Quantity"] * f["Price_in_gold"]
            e[g]["items"].append(f["Item"])

        h = sorted(e, key=lambda x: int(x) if str(x).isdigit() else str(x))

        d = {}

        for f in h:
            e[f]["items"].sort(reverse=True)
            d[f"group{f}"] = e[f]

    with open(json_filename, "w", encoding="utf-8") as e:
        json.dump(d, e, ensure_ascii=False, indent=2)