Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def count_word_occurrences(file_path, target_word):
- with open(file_path, 'r') as file:
- text = file.read()
- # Удаляем знаки пунктуации из текста (за исключением букв и цифр)
- punctuation_marks = '''!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~'''
- text = text.translate(str.maketrans('', '', punctuation_marks))
- words = text.split()
- word_count = sum(1 for word in words if word.lower() == target_word.lower())
- return word_count
- # Пример использования
- file_path = 'file.txt' # Путь к текстовому файлу
- target_word = 'example' # Заданное пользователем слово
- occurrences = count_word_occurrences(file_path, target_word)
- print(f"Слово '{target_word}' встречается {occurrences} раз(а) в файле.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement