Python Forum
finding p's in words of a multi-line text file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
finding p's in words of a multi-line text file
#1
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.
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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']
Reply
#4
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.
Reply
#5
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)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading a text until matched string and print it as a single line cananb 1 2,017 Nov-29-2020, 01:38 PM
Last Post: DPaul
  Delete line of file dionatandiego11 2 1,899 Aug-05-2020, 05:40 PM
Last Post: deanhystad
  Seven Segment Display - QUERY on Multi-line side-by-side printing output ajayachander 3 4,824 Mar-13-2020, 07:02 AM
Last Post: ajayachander
  Convert text from an image to a text file Evil_Patrick 5 4,268 Jul-30-2019, 07:57 PM
Last Post: DeaD_EyE
  reading text file and writing to an output file precedded by line numbers kannan 7 10,368 Dec-11-2018, 02:19 PM
Last Post: ichabod801
  write split words of sentence to file bluefrog 1 2,977 Aug-27-2018, 01:28 AM
Last Post: micseydel
  Finding the average of numbers in a txt file piday 1 19,191 Feb-27-2018, 04:00 AM
Last Post: Larz60+
  Command line inputs not printing to Log File ijosefson 1 3,348 Oct-19-2017, 06:41 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020