Reputation: 11
So I'm reading in a txt file in python and want to get a list of all the words in the file. I am doing so by this method.
def parse_in(file_location, input_name):
all_words = []
with open(file_location + input_name,'r') as f:
for line in f:
for word in line.split():
all_words += word
f.close
return all_words
Instead of all_words returning actual words it returns a list of single characters like ['A', 'B', 'C']
and so on.
But when debugging I can see that the variable word is an actual word and not just characters. Where am I going wrong here?
Upvotes: 0
Views: 87
Reputation: 17322
if you use context manager you do not need to close the file, and for simplicity you could just read entire file and just use split method
def parse_in(file_location, input_name):
all_words = []
with open(file_location + input_name,'r') as f:
all_words = f.read().split()
return all_words
Upvotes: 0
Reputation: 2546
all_words += word
extends the list all_words
with the items in word
. As word
is a string, the characters are appended, as if you had iterated through it with for
.
Use all_words.append(word)
to append the word to the word list.
Upvotes: 0
Reputation: 59095
Use
all_words.append(word)
instead of
all_words += word
If you use +=
then word
is treated as a sequence, so the individual characters are each appended to the list.
Alternatively, you can use +=
, and simplify your loop to:
for line in f:
all_words += line.split()
Upvotes: 3
Reputation: 3346
try this:
def parse_in(file_location, input_name):
all_words = []
with open(file_location + input_name,'r') as f:
for line in f:
all_words += line.split()
return all_words
Upvotes: 0