Python Forum

Full Version: finding p's in words of a multi-line text file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Here's the assignment given:

Using the file school_prompt.txt, if the character ‘p’ is in a word, then add the word to a list called p_words. (I will post text file at bottom of this post.)

Here's my attempt:
write = open("school_prompt.txt", "r")
p_words = []
for aline in write:
    items = aline.split()
    if "p" in items:
        p_words.append(items)
print(p_words)
write.close()
Output:
My result: [] Expected result: ['topic', 'point', 'papers', 'ups', 'scripts']
I'm not sure where I went wrong, but it feels like I'm only getting the first line of text (no 'p' in first line).

If interested in school_prompt.txt, see below.

Writing essays for school can be difficult but
many students find that by researching their topic that they
have more to say and are better informed. Here are the university
we require many undergraduate students to take a first year writing requirement
so that they can
have a solid foundation for their writing skills. This comes
in handy for many students.
Different schools have different requirements, but everyone uses
writing at some point in their academic career, be it essays, research papers,
technical write ups, or scripts.


I bolded the 'p' words.
Line 4 makes items a list of words in the line. That means the test on line 5 is only going to be true if a word containing only one letter 'p' is in the line. You need to loop over the words in the line (items) and run that test on each word.
Still need help with this one.
My second attempt, trying to take Ichabod's advice.
But I think I may have screwed up the code even more.
In particular, I need help iteration over lines and then iteration over items in lines.

Here's my new code:
write = open("school_prompt.txt", "r")
p_words = []

#splitting text into lines
for aline in write.readlines():
    items = aline.split()

#splitting lines into words
    for words in items:
        word = words.split()

#testing words for "p"
        if "p" in word:
            p_words.append(word)

print(p_words)
write.close()
This, however still just gets me the result of: []
when the expected result is: ['topic', 'point', 'papers', 'ups', 'scripts']
If you put a print(items) in after items = aline.split() you would see that items contains a list of words for each row read from the text file.
Output:
['Writing', 'essays', 'for', 'school', 'can', 'be', 'difficult', 'but'] ['many', 'students', 'find', 'that', 'by', 'researching', 'their', 'topic', 'that', 'they'] ['have', 'more', 'to', 'say', 'and', 'are', 'better', 'informed.', 'Here', 'are', 'the', 'university'] ['we', 'require', 'many', 'undergraduate', 'students', 'to', 'take', 'a', 'first', 'year', 'writing', 'requirement'] ['so', 'that', 'they', 'can'] ['have', 'a', 'solid', 'foundation', 'for', 'their', 'writing', 'skills.', 'This', 'comes'] ['in', 'handy', 'for', 'many', 'students.'] ['Different', 'schools', 'have', 'different', 'requirements,', 'but', 'everyone', 'uses'] ['writing', 'at', 'some', 'point', 'in', 'their', 'academic', 'career,', 'be', 'it', 'essays,', 'research', 'papers,'] ['technical', 'write', 'ups,', 'or', 'scripts.'] ['topic', 'point', 'papers,', 'ups,', 'scripts.']
That means word = words.split() is not required because you only need now to loop over items to get each word and then check for 'p' in word.
You don't want split the word. Say you do 'Spam spam spam and eggs'.split(). That will give you a list: ['Spam', 'spam', 'spam', 'and', 'eggs']. That's the sort of thing you have in items. That's the sort of thing you have in items after line 6. Then you loop over words, but each time through that loop, 'words' is only one word. The first time through that loops, words is 'Spam'. But on line 10 you split it again, giving you the list ['Spam']. When you check for 'p' in word, it is checking that list, and 'p' is not in that list. You don't want to do the second split, you just want to check for 'p':

for line in write:   # (also, you don't need readlines)
    words = line.split()
    for word in words:
        if 'p' in word:
            p_words.append(word)
This code is unpythonic.
(Jun-21-2019, 02:14 AM)johneven Wrote: [ -> ]
write = open("school_prompt.txt", "r")
for aline in write:
    ...
write.close()
This is the pythonic way
with open("school_prompt.txt", "r") as file:
    for aline in file:
        ....
and btw why do you call a variable reading a file 'write' ?
Please explain that in detail, i´m really curious.