I need some help figuring out how to split the words in a text file into a list. I can use something like this:
words = []
for line in open('text.txt'):
line.split()
words.append(line)
But if the file contains multiple lines of text, they are split into sublists, e.g.
this is the first line
this is the second line
Becomes:
[['this', 'is', 'the', 'first', 'line'], ['this', 'is', 'the', 'second', 'line']]
How do I make it so that they are in the same list? i.e.
[['this', 'is', 'the', 'first', 'line', 'this', 'is', 'the', 'second', 'line']]
thanks!
EDIT: This program will be opening multiple text files, so the words in each file need to be added to a sublist. So if a file has multiple lines, all the words from these lines should be stored together in a sublist. i.e. Each new file starts a new sublist.