0

I am searching a text file for an input word. However, I am only meant to search the text in the file after the word "START". The first twenty-odd before "START" should be ignored. I know how to find "START", but not how to search the rest of the file once "START" is encountered. I would appreciate any guidance!

Here is what I have so far:

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

print("***** Anagram Finder *****")
word = input("Enter a word: ")


for line in file:
    if "START" in line:
        if word in line:
            print("Yes, ", word, " is in the file.", sep="")
        else:
            print("Sorry, ", word, " is not in the file.", sep="")


file.close()

Here is a sample of the text file:

    The name of Princeton University or Princeton may not be
     used in advertising or publicity pertaining to
     distribution of the software and/or database.  Title to
     copyright in this software, database and any associated
     documentation shall at all times remain with Princeton
     University and LICENSEE agrees to preserve same.
START
clobber
transversalis
squinter
cunner
damson
extrovertive
absorptive
jdvcal
  • 37
  • 1
  • 7
  • iterate how? Word by word? Line by line? Basically you read until you find START, then keep reading. How you continue depends on how you got to START. I.e., ... what have you got so far? – alexis Jun 15 '17 at 13:18
  • Could you [edit your question](https://stackoverflow.com/posts/44568438/edit) to include a sample of your text file and what you a trying to find in it. – Martin Evans Jun 15 '17 at 13:21
  • Concur with @MartinEvans. What is the file format/style? Is it line by line? Or is it paragraph styled? That would change the scan method. – pstatix Jun 15 '17 at 13:24
  • I have edited to include my attempts so far... – jdvcal Jun 15 '17 at 13:24
  • Possible duplicate of [this](https://stackoverflow.com/questions/27805919/how-to-only-read-lines-in-a-text-file-after-a-certain-string-using-python)? – pstatix Jun 15 '17 at 13:31

6 Answers6

1

Modifying your code, we have

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

print("***** Anagram Finder *****")
word = input("Enter a word: ")


start_looking = False
word_found = False

for line in file:
    if not start_looking:
        if "START" in line:
            start_looking = True
        else:
            continue

    if word in line:
        print("Yes, ", word, " is in the file.", sep="")
        word_found = True
       break

if not word_found:
    print("Sorry, ", word, " is not in the file.", sep="")

file.close()

As long as START hasn't been found, keep skipping over the lines of the file. If, however, you encounter START, reset your flag and begin looking.

cs95
  • 379,657
  • 97
  • 704
  • 746
0

What about something with regex module ?

re.findall(r"START.*(word_to_search).*", entire_text)

This should return you the result only if there is a START before the word to search for. I hope that's what you're looking for.

EDIT : For a solution line by line i would go with something like :

start_search = 0
    with open(bigfile, "r") as f:
        for line in f:
            if "START" IN line:
                start_search = 1
            if start_search and word_to_search in line:
                print("result foun")
                return (word_to_search)

What about this ?

Pablo
  • 217
  • 1
  • 10
0

Do a for after your word is found:

with open(myfile, 'r') as f:
    for line in f:
        if 'START' in line:
            # do stuff to lines below 'START'
            # you could do another for loop here to iterate
            for line in f:
                print (line) # just an example

Very similar to this other SO post. Credit for the syntax of my answer comes from its answer.

pstatix
  • 3,611
  • 4
  • 18
  • 40
0

Keep it short, simple and explicit:

with open("EnglishWords.txt", 'r') as fin:
    output = fin.readlines()
    # Find the line that contains START
    index = output.index("START")
    # Search all the lines after that
    for line in output[index+1:]:
        if word in line:
            print("Yes, ", word, " is in the file.", sep="")
        else:
            print("Sorry, ", word, " is not in the file.", sep="")
ILostMySpoon
  • 2,399
  • 2
  • 19
  • 25
0

You could use Python's dropwhile() to locate the start of the words and iterate from there:

from itertools import dropwhile

print("***** Anagram Finder *****")
word = input("Enter a word: ").lower() + '\n'

with open("EnglishWords.txt") as f_words:
    if word in dropwhile(lambda r: not r.startswith("START"), f_words):
        print("Yes, {} is in the file".format(word.strip()))
    else:
        print("Sorry, {} is not in the file.".format(word.strip()))
Martin Evans
  • 45,791
  • 17
  • 81
  • 97
-1

You can use a boolean :

file = open(“testfile.txt”, “r”) 
foundStart = False
for line in file: 
    if foundStart:
         # do something...
    elif line == "START":
       foundStart = True
Xatyrian
  • 1,364
  • 8
  • 26