/
First_user
/
Working_with_files
Обзор
Документация
Войти
/
First_user
/
Working_with_files
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Read-Write.py
91 строка
3 KB
Child-of-the-steppes
Initial commit
13 янв 2026, 22:31
13 янв 2026, 22:31
8f6cec9
Код
Авторство
О чём код?
import os import pprint # TASK 1 # def read_cook_book(file_path): # cook_book = {} # with open(file_path, 'r', encoding='utf-8') as file: # lines = file.readlines() # i = 0 # while i < len(lines): # line = lines[i].strip() # if not line: # i += 1 # continue # dish_name = line # i += 1 # if i >= len(lines): # break # try: # ingredient_count = int(lines[i].strip()) # i += 1 # except ValueError: # continue # ingredients = [] # for _ in range(ingredient_count): # if i >= len(lines): # break # ingredient_line = lines[i].strip() # parts = ingredient_line.split('|') # if len(parts) == 3: # ingredient_name = parts[0].strip() # try: # quantity = int(parts[1].strip()) # except ValueError: # i += 1 # continue # measure = parts[2].strip() # ingredients.append({ # 'ingredient_name': ingredient_name, # 'quantity': quantity, # 'measure': measure # }) # i += 1 # cook_book[dish_name] = ingredients # return cook_book # cook_book = read_cook_book('recipes.txt') # # pprint.pprint(cook_book) #TASK2 # def get_shop_list_by_dishes(dishes, person_count, cook_book): # shop_list = {} # for dish in dishes: # if dish in cook_book: # for ingredient in cook_book[dish]: # name = ingredient['ingredient_name'] # measure = ingredient['measure'] # quantity = ingredient['quantity'] * person_count # if name in shop_list: # shop_list[name]['quantity'] += quantity # else: # shop_list[name] = {'measure': measure, 'quantity': quantity} # return shop_list # result = get_shop_list_by_dishes(['Запеченный картофель', 'Омлет'], 2, cook_book) # # pprint.pprint(result) #TASK3 def merge_files(file_list, output_file): data = [] for filename in file_list: with open(filename, 'r', encoding='utf-8') as f: lines = f.readlines() line_count = len(lines) content = ''.join(lines) data.append((filename, line_count, content)) data.sort(key=lambda x: x[1]) with open(output_file, 'w', encoding='utf-8') as out: for filename, count, content in data: out.write(f"{filename}\n") out.write(f"{count}\n") out.write(f"{content}\n") files = ['1.txt', '2.txt', '3.txt'] merge_files(files, 'result.txt')