/
Denis86
/
File1
Обзор
Документация
Войти
/
Denis86
/
File1
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
file.py
49 строк
1 KB
Denis86
upload files
18 июн 2025, 19:44
18 июн 2025, 19:44
b656ca6
Код
Авторство
О чём код?
def parse_ingredient_line(line): name, quantity, measure = map(str.strip, line.split('|')) return { 'ingredient_name': name, 'quantity': int(quantity), 'measure': measure } def read_recipes(file_path): cook_book = {} with open('recipes.txt', 'r', encoding='utf-8') as file: while True: dish_name = file.readline().strip() if not dish_name: break ingredient_count = int(file.readline().strip()) ingredients = []+ for _ in range(ingredient_count): line = file.readline().strip() if line: ingredients.append(parse_ingredient_line(line)) cook_book[dish_name] = ingredients file.readline() return cook_book def print_cook_book(cook_book): print('{') for dish, ingredients in cook_book.items(): print(f" '{dish}': [") for ing in ingredients: print(f" {ing},") print(" ],") print('}') def main(): try: cook_book = read_recipes('recipes.txt') print_cook_book(cook_book) except FileNotFoundError: print("Ошибка: файл 'recipes.txt' не найден!") except Exception as e: print(f"Произошла ошибка: {e}") if __name__ == '__main__': main()