Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7499679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:54:20+00:00 2026-05-29T19:54:20+00:00

I am wondering if there is a way in Python 2.7 to read through

  • 0

I am wondering if there is a way in Python 2.7 to read through a text document to look for certain words, and then if it finds those words, it will read a few lines down to look for a new word. If it doesn’t find the new word, it will go back to looking for the first word.

For example my text doc looks like this:

1.Section

2. Cheesecake
3. 0
4. 0
5. 0
6. Tastes good
7. 0
8. 0
9. Donut
10. 0
11. Tastes good
12. 0
13. Cheesecake
14. 0
15. 0
16. Tastes bad

This is the code I have so far:

import sys

fileName = open("filename.txt", "r")

while True:
    line = fileName.readline()
    if line is None: break
    else:
        if line.startswith('CHEESECAKE'):
            print (line)
            x = raw_input(" Continue? ")
            if x == "n":
                sys.exit()

I don’t know what to do from this point on! How can I get it to search for cheesecakes and then check if they taste good?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T19:54:22+00:00Added an answer on May 29, 2026 at 7:54 pm

    You can also group the records from one key word to the next.

    import sys

    def process_group(list_in):
    if len(list_in):
    for rec in list_in:
    if rec.startswith("Tastes"):
    print "Tastes found for", list_in[0]
    return
    print "Tastes NOT found for", list_in[0]

    open_file = open("filename.txt", "r")

    group_list=[]
    start_list=["CHEESECAKE", "DONUT"]
    for line in open_file:
    for st in start_list:
    if line.upper().startswith(st): ## process this group
    print (line)
    process_group(group_list)
    group_list=[] ## an empty list for the next group
    x = raw_input(" Continue? ")
    if x == "n":
    sys.exit()
    group_list.append(line)

    process_group(group_list) # process the last group

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Python 2.x and I'm wondering if there's a way to tell if
I was wondering, if there is way in Python to modify collections without creating
I was wondering if there's a way in Python(2.6) to get only the name
I was wondering if there's a way to plot a data cube in Python.
Is there a way to write python data structs to a file as text.
I am wondering whether there's a lightweight way to distribute python tasks among different
I was wondering if there is a way to get vim to read .gitignore
I was wondering if there is any way to export python functions to dll.
I was wondering if there is way to open another page using a Modal
I was wondering is there a way in PHP that you could tell where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.