JustCaptain kod-buduschego
llm/change-52ed52
Перейти к файлу
Код
kod-buduschego Python 2025-20263.6. Работа с файламиpracticeschatgpttask_2main.py
anc update c70a58f 8 октября 2026 г.
main.py
"""
2. Анализ переписки детектива

Контекст: детектив собирает подозрительные сообщения.

Файл: messages.txt

Задачи:

- Найти все строки со словами: money, secret, meeting.
- Посчитать, сколько раз встречается каждое слово.
- Скрыть имена людей (John → \\*\\*\\*) и сохранить в новый файл.

"""


def research_message(path: str, search_words: list[str], zip_names: list[str]) -> None:
    with open(file=path, mode="r", encoding="utf-8") as file:
        lines: list[str] = file.readlines()
    count: dict[str, int] = {}
    result: str = ""
    for line in lines:
        for zip_name in zip_names:
            if line.count(f"{zip_name}:"):
                line = line.replace(f"{zip_name}:", "\\*\\*\\*")
        result += line
        for word in search_words:
            split_line: list[str] = line.split(" ")
            if word in split_line:
                if count.get(word):
                    count[word] = count[word] + 1
                else:
                    count[word] = 1
    with open("zip_message.txt", "w", encoding="utf-8") as file:
        file.write(result)
    print(count)


research_message(
    path="messages.txt",
    search_words=["money", "secret", "meeting"],
    zip_names=["John", "Sarah", "Mike", "Emma", "Alex"],
)