diff --git a/zadanie_1.py b/zadanie_1.py index 3ba73d8..0a062ad 100644 --- a/zadanie_1.py +++ b/zadanie_1.py @@ -2,33 +2,35 @@ import os # 1. Загрузка заказов из файла def load_orders(filepath: str) -> list[dict]: - """ - Читает файл с заказами. - Формат строки: user_id, item_name, quantity, price_per_item - """ orders = [] - with open(filepath, 'r', encoding='utf-8') as file: - for line in file: - line = line.strip() - if not line: - continue - parts = line.split(',') - if len(parts) != 4: - continue - - user_id = int(parts[0].strip()) - item_name = parts[1].strip() - quantity = int(parts[2].strip()) - price = float(parts[3].strip()) - total = quantity * price - - orders.append({ - 'user_id': user_id, - 'item_name': item_name, - 'quantity': quantity, - 'price': price, - 'total': total - }) + try: + with open(filepath, 'r', encoding='utf-8') as file: + for line in file: + line = line.strip() + if not line: + continue + parts = line.split(',') + if len(parts) != 4: + continue + try: + user_id = int(parts[0].strip()) + item_name = parts[1].strip() + quantity = int(parts[2].strip()) + price = float(parts[3].strip()) + except ValueError: + continue + total = quantity * price + orders.append({ + 'user_id': user_id, + 'item_name': item_name, + 'quantity': quantity, + 'price': price, + 'total': total + }) + except FileNotFoundError: + print(f"Ошибка: файл {filepath} не найден.") + except Exception as e: + print(f"Ошибка при чтении файла: {e}") return orders @@ -156,7 +158,7 @@ def save_report(filepath: str, report_text: str) -> bool: def main(): print("=== Система анализа заказов интернет-магазина ===\n") - data_dir = r"C:\Users\Ardor\Desktop\Практика\others" + data_dir = "data" # Создаем папку, если её нет os.makedirs(data_dir, exist_ok=True)