Advertisement
plarmi

workpython_14_2

Jul 9th, 2023
591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.49 KB | None | 0 0
  1. def count_characters(file_path):
  2.     with open(file_path, 'r') as file:
  3.         text = file.read()
  4.        
  5.         char_count = len(text)
  6.         line_count = text.count('\n')
  7.         vowel_count = sum(1 for char in text if char.lower() in 'aeiouаеёиоуыэюя')
  8.         consonant_count = sum(1 for char in text if char.isalpha() and char.lower() not in 'aeiouаеёиоуыэюя')
  9.         digit_count = sum(1 for char in text if char.isdigit())
  10.        
  11.         return char_count, line_count, vowel_count, consonant_count, digit_count
  12.  
  13.  
  14. def write_statistics(file_path, statistics):
  15.     with open(file_path, 'w') as file:
  16.         file.write("Статистика по исходному файлу:\n")
  17.         file.write(f"Количество символов: {statistics[0]}\n")
  18.         file.write(f"Количество строк: {statistics[1]}\n")
  19.         file.write(f"Количество гласных букв: {statistics[2]}\n")
  20.         file.write(f"Количество согласных букв: {statistics[3]}\n")
  21.         file.write(f"Количество цифр: {statistics[4]}\n")
  22.  
  23. # Пример использования
  24. input_file_path = 'input.txt'      # Путь к исходному файлу
  25. output_file_path = 'statistics.txt'  # Путь к файлу со статистикой
  26.  
  27. statistics = count_characters(input_file_path)
  28. write_statistics(output_file_path, statistics)
  29.  
  30. print("Статистика успешно записана в файл.")
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement