lab2
This commit is contained in:
@@ -0,0 +1,62 @@
|
|||||||
|
def process_line(text: str) -> str:
|
||||||
|
if not text:
|
||||||
|
return text
|
||||||
|
|
||||||
|
result = [text[0]]
|
||||||
|
|
||||||
|
for i in range(1, len(text)):
|
||||||
|
current_char = text[i]
|
||||||
|
last_written = result[-1]
|
||||||
|
|
||||||
|
if current_char == last_written:
|
||||||
|
result.append(" ")
|
||||||
|
else:
|
||||||
|
result.append(current_char)
|
||||||
|
|
||||||
|
return "".join(result)
|
||||||
|
|
||||||
|
|
||||||
|
def ask_file_name(prompt: str) -> str:
|
||||||
|
while True:
|
||||||
|
file_name = input(prompt).strip()
|
||||||
|
if file_name:
|
||||||
|
return file_name
|
||||||
|
print("Ошибка: имя файла не может быть пустым. Повторите ввод.")
|
||||||
|
|
||||||
|
|
||||||
|
def read_lines_with_retry() -> tuple[str, list[str]]:
|
||||||
|
while True:
|
||||||
|
input_file = ask_file_name("Введите имя входного файла: ")
|
||||||
|
try:
|
||||||
|
with open(input_file, "r", encoding="utf-8") as fin:
|
||||||
|
return input_file, fin.readlines()
|
||||||
|
except (OSError, UnicodeError) as error:
|
||||||
|
print(f"Ошибка чтения файла '{input_file}': {error}")
|
||||||
|
print("Попробуйте снова.")
|
||||||
|
|
||||||
|
|
||||||
|
def write_lines_with_retry(lines: list[str]) -> str:
|
||||||
|
while True:
|
||||||
|
output_file = ask_file_name("Введите имя выходного файла: ")
|
||||||
|
try:
|
||||||
|
with open(output_file, "w", encoding="utf-8") as fout:
|
||||||
|
fout.writelines(lines)
|
||||||
|
return output_file
|
||||||
|
except (OSError, UnicodeError) as error:
|
||||||
|
print(f"Ошибка записи файла '{output_file}': {error}")
|
||||||
|
print("Попробуйте снова.")
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
_, lines = read_lines_with_retry()
|
||||||
|
|
||||||
|
processed_lines = [process_line(line) for line in lines]
|
||||||
|
output_file = write_lines_with_retry(processed_lines)
|
||||||
|
|
||||||
|
print("Обработка завершена.")
|
||||||
|
print(f"Результат записан в файл {output_file}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|
||||||
Reference in New Issue
Block a user