![]() |
finding p's in words of a multi-line text file - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: finding p's in words of a multi-line text file (/thread-19279.html) |
finding p's in words of a multi-line text file - johneven - Jun-21-2019 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() 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. RE: finding p's in words of a multi-line text file - ichabod801 - Jun-21-2019 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. RE: finding p's in words of a multi-line text file - johneven - Jun-23-2019 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'] RE: finding p's in words of a multi-line text file - Yoriz - Jun-23-2019 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. 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.
RE: finding p's in words of a multi-line text file - ichabod801 - Jun-23-2019 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) RE: finding p's in words of a multi-line text file - ThomasL - Jun-24-2019 This code is unpythonic. (Jun-21-2019, 02:14 AM)johneven Wrote:This is the pythonic waywrite = open("school_prompt.txt", "r") for aline in write: ... write.close() 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. |