Last active
May 11, 2018 02:01
-
-
Save AlexDel/ebe2db3aa2e709fadad0e33de43e292b to your computer and use it in GitHub Desktop.
Примерный код программы для зачета
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Открываем файл | |
fileWithText = open('someFile.txt', 'r', encoding='utf-8') | |
# Если появляется ошибка с текстов "Unicode error, can't decode..." используйте это - "fileWithText = open('someFile.txt', 'r', encoding='cp1251')" | |
# Читаем содержимое файла. Записываем строку в переменную textFromFile | |
textFromFile = fileWithText.read() | |
# Функция, которая ищет слова, которые начинаются с 'un' - unbreak, undone, etc. | |
def countWordsWithUn(textString): | |
# Разделяем строку по пробелам, чтобы получить список слова | |
words = textString.split() | |
# Инициализируем пустой список куда будем складвать слова | |
wordsWithUn = [] | |
# Проходим циклом по каждому слову | |
for word in words: | |
# Если первые 2 символа в слове - un, то добавляем слово в массив | |
if word[0:2] == 'un': | |
wordsWithUn.append(word) | |
# Возращаем список слов | |
return wordsWithUn | |
# Получаем слова с un из текста из нашего файла | |
print(countWordsWithUn(textFromFile)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment