user998316
user998316

Reputation: 10783

append word from line when reading txt file python

I am trying to create a program that will read a text file and create a list of lines of words.

However am only able to append each line and not word, any help would be appreciated with this problem.

text = open("file.txt","r")

for line in text.readlines():
    sentence = line.strip()
    list.append(sentence)

    print list 
text.close()

Example text

I am here
to do something

and I wanted it to append it like this

[['I','am','here']['to','do','something']]

Thanks in advance.

Upvotes: 0

Views: 4100

Answers (6)

Nate
Nate

Reputation: 12829

Something like this would cover a large number of cases, and could be tailored to your used symbols:

import re
text = open("file.txt","r")

for line in text.readlines():
    sentence = line.strip()
    words = re.sub(" +"," ",re.sub("[^A-Za-z']"," ",sentence)).split()
    somelist.append(words)

    print list 
text.close()

This would only include the capital and lower case letters and apostrophes (for the sake of contractions)

Upvotes: 1

johnsyweb
johnsyweb

Reputation: 141810

It looks like you're just missing a call to str.split(). Here's a simple a one-line list comprehension that does what you have asked for:

>>> [line.split() for line in open('file.txt')]
[['i', 'am', 'here'], ['to', 'do', 'something']]

Upvotes: 0

John La Rooy
John La Rooy

Reputation: 304205

>>> with open("file.txt","r") as f:
...     map(str.split, f)
... 
[['i', 'am', 'here'], ['to', 'do', 'something']]

Upvotes: 1

Russell Dias
Russell Dias

Reputation: 73292

Where exactly are you getting the y variable?

In the most basic sense (because you have not quite specified what to do with punctuation) you can split each line into a list of words using line.split(' '), which splits on every space. If you have other delimiters you can substitute that in, instead of the space. Assign the above split to a var if need be and append it to your list.

@Brendan has provided a good solution to strip basic punctuation. Alternatively, you could also use a simple regex re.findall(r'\w+', file) to find all words in a given file.

Using yet another way, you can take advantage of pythons string library, and string.punctuation in particular:

str = list(line)
''.join([ word for word in str if not word in string.punctuation ]).split()

Upvotes: 1

Brendan
Brendan

Reputation: 19353

Each line in the example is just a string, so something like,

...
    PUNCTUATION = ',.?!"\''
    words = [w.strip(PUNCTUATION) for w in line.split() if w.strip(PUNCTUATION)]
    list.append(words)
...

would probably be okay to the first approximation although may not cover every edge case in the way that you want (i.e. hyphenated words, words not separated by whitespace, words that have a trailing apostrophe etc.)

The conditional is to avoid blank entries.

Upvotes: 1

Acorn
Acorn

Reputation: 50497

text = open("file.txt","r")

word_groups = []

for line in text.readlines():
    words = line.strip().split(' ')
    word_groups.append(words)

print word_groups

text.close()

Upvotes: 0

Related Questions