我有一个名为test.txt
. 我想阅读它并从文件中返回所有单词的列表(删除了换行符)。
这是我当前的代码:
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()
运行此代码会生成此列表:
['hello there how is everything ', 'thank you all', 'again', 'thanks a lot']
我希望列表看起来像这样:
['hello','there','how','is','everything','thank','you','all','again','thanks','a','lot']