retornando uma lista de palavras depois de ler um arquivo em python
Eu tenho um arquivo de texto que é chamadotest.txt
. Eu quero lê-lo e retornar uma lista de todas as palavras (com novas linhas removidas) do arquivo.
Este é o meu código atual:
def read_words(test.txt):
open_file = open(words_file, 'r')
words_list =[]
contents = open_file.readlines()
for i in range(len(contents)):
words_list.append(contents[i].strip('\n'))
return words_list
open_file.close()
Executar este código produz esta lista:
['hello there how is everything ', 'thank you all', 'again', 'thanks a lot']
Eu quero que a lista fique assim:
['hello','there','how','is','everything','thank','you','all','again','thanks','a','lot']