From 4a45166c01f8d7d2aaa9802cd4db6f7e6a5d2108 Mon Sep 17 00:00:00 2001 From: teabag Date: Sat, 18 Apr 2026 10:30:20 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BD?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BF=D0=B5=D1=80=D0=B2=D0=B0=D1=8F=20=D1=84?= =?UTF-8?q?=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8F=20load=5Fdata(filepath:=20?= =?UTF-8?q?str)=20->=20list[dict]:?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 3342c3b..2c94bc6 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,44 @@ # 1. Читает текстовый файл с данными (каждая строка: имя,отдел,зарплата,стаж). # Возвращает список словарей: [{"name": str, "dept": str, "salary": float, "exp": float}, ...]. +from sys import exec_prefix + def load_data(filepath: str) -> list[dict]: - pass + employees = [] + filepath = "data/employees.txt" + try: + with open(filepath, "r", encoding="utf-8") as file: + for line_number, line in enumerate(file, 1): + # Чищу строку от пробелов по краям и скипаю пустые строки, а то хз кто там этот емплоес.тииксти писал + line = line.strip() + if not line: + continue + + try: + # делим строку по запятой + parts = [p.strip() for p in line.split(",")] # зачем нейронки если всю эту строку мне пайчарм вставил после знака равно??? + + if len(parts) != 4: # ну вдруг там не 4 колонки + print(f"Ошибка в строке {line_number}: надо 4 значения, а получил {len(parts)}.") + continue + + employee = { + "name": parts[0], + "dept": parts[1], + "salary": float(parts[2]), + "exp": float(parts[3]) + } + employees.append(employee) + + except ValueError as e: + print(f"Ошибка преобразования типов в строке {line_number}: {e}") + + except FileNotFoundError: + print(f"Ошибка: Файл по адресу '{filepath}' не найден.") + except Exception as e: + print(f"Произошла непредвиденная ошибка: {e}") + + return employees + # 2. Возвращает новый список, содержащий только сотрудников указанного отдела. def filter_by_department(employees: list[dict], department: str) -> list[dict]: