0

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?

1
  • 1
    f.close does nothing. Your adding a sequence (string) to another sequence (a list), so you get a sequence thats the combination of both sequences. Use list.append. Commented Feb 13, 2019 at 11:49

4 Answers 4

3

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()
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.