Mam plik tekstowy o nazwie test.txt
. Chcę go przeczytać i zwrócić listę wszystkich słów (z usuniętymi znakami nowej linii) z pliku.zwraca listę słów po przeczytaniu pliku w pythonie
To jest mój bieżący kod:
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()
Uruchomienie tego kodu produkuje tę listę:
['hello there how is everything ', 'thank you all', 'again', 'thanks a lot']
Chcę listę, aby wyglądać tak:
['hello','there','how','is','everything','thank','you','all','again','thanks','a','lot']
http://docs.python.org/2/library/stdtypes.html#str.split – kreativitea