I have a dictionary file.txt, with 3 - 5 words in each line.
Like:
apple ball back hall
hello bike like
I need a python code to put all these words into a list before any other coding occurs.
My idea is to read the file line-by-line, and use split()
, and list.append()
functions, but I don't seem to sort it out. Can anyone help?
Edited: Said it the wrong way, sorry!
You can do something like:
file_list = []
with open('file.txt', 'r') as file:
for line in file.readlines().rstrip('\n'):
for word in line.split(' '):
file_list.append(word)
If file.txt
has the following contents:
apple ball back hall
hello bike like
The output would be:
>>> file_list
['apple', 'ball', 'back', 'hall', 'hello', 'bike', 'like']