私はtest.txt
という名前のテキストファイルを持っています。私はそれを読んで、ファイルからすべての単語のリスト(改行を削除したもの)を返したいと思います。pythonでファイルを読み込んだ後に単語のリストを返す
これは私の現在のコードです:このコードを実行する
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']
http://docs.python.org/2/library/stdtypes.html#str分割 – kreativitea