Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
.txt ファイルからすべての単語を取得し、各単語をリストの要素としてリストに入れたいと思います。.txt では、単語は改行で区切られています。これまでの私のコードは次のとおりです。
with open('words.txt', "r") as word_list: words = list(word_list.read())
ただし、このコードは、.txt の各文字を独自の要素としてリストに配置するだけです。何か案は?
with open('words.txt', "r") as word_list: words = word_list.read().split(' ')
取り除く.read():
.read()
words = list(word_list)
がなければ.read()、ファイル ハンドルをリストに変換することになり、行のリストが得られます。を使用.read()すると、文字の大きなリストが得られます。