一旦找到单词,如何遍历文件

时间:2017-06-15 13:10:26

标签: python python-3.x file

我正在搜索文本文件中的输入字。但是,我只想在单词“START”之后搜索文件中的文本。应该忽略“START”之前的前二十多个。我知道如何找到“START”,但不知道如何在遇到“START”时搜索文件的其余部分。我很感激任何指导!

这是我到目前为止所做的:

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()

以下是文本文件的示例:

    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

6 个答案:

答案 0 :(得分:1)

修改代码,我们有

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()

只要找不到START,就不断跳过文件的行。但是,如果您遇到START,请重置您的旗帜并开始查看。

答案 1 :(得分:0)

使用正则表达式模块怎么样?

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

只有在要搜索的单词之前有START时,才会返回结果。我希望你能找到的是什么。

编辑: 对于逐行解决方案,我会使用类似的东西:

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)

这个怎么样?

答案 2 :(得分:0)

在找到您的单词后执行for

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

非常类似this其他SO帖子。我的答案的语法归功于它的答案。

答案 3 :(得分:0)

简短,简洁明了:

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="")

答案 4 :(得分:0)

您可以使用Python dropwhile()找到单词的开头并从那里进行迭代:

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()))

答案 5 :(得分:-1)

您可以使用布尔值:

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