如何在发现一个单词时遍历文件

问题描述:

我正在为输入单词搜索文本文件。但是,我只是想在文字“开始”之后搜索文件中的文本。 “开始”之前的第一个二十几个应该被忽略。我知道如何查找“开始”,但不知道如何在遇到“开始”时搜索文件的其余部分。我将不胜感激任何指导!如何在发现一个单词时遍历文件

这是我到目前为止有:

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 
+3

你尝试过这么远吗? – ILostMySpoon

+0

迭代如何?逐词地?逐行?基本上,你阅读,直到你找到开始,然后继续阅读。你如何继续取决于你如何开始。也就是说,你到目前为止有什么? – alexis

+0

你可以[编辑你的问题](https://stackoverflow.com/posts/44568438/edit)包含你的文本文件的样本以及你试图在其中找到的东西。 –

做你的字后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发布。我的答案的语法信用来自它的答案。

+0

谢谢,这是最好的解决方案! – jdvcal

什么东西用正则表达式模块?

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) 
这个

什么?

+0

这是一个优雅的解决方案,但如果文件很大,应该逐行阅读呢? – Ding

+0

该文件并不是那么庞大,但我需要逐行读取它才能完成作业...... – jdvcal

+0

谢谢!(我太低级+1)。 – jdvcal

您可以使用一个布尔值:

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

OP已经说他知道如何找到“START”... – ILostMySpoon

+0

那么,他没有分享他的代码,所以我给了另一种方式来找到它;) – Xatyrian

+0

@Xatyrian我不认为给另一种方式做他的事情他对于他的问题,他已经知道有资格成为_answer_。 :) – Amous

修改你的代码,我们有

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,请重置您的旗帜并开始寻找。

+0

这将在“开始”后的每一行打印'“对不起......”'直到找到该单词。 – asongtoruin

+0

@ason​​gtoruin啊,谢谢你指出。重点是其他错误,没有看到这一个。 –

+1

不用担心 - 我在你之后发布了几乎相同的答案,但删除了,因为你对'continue'的使用比我使用的第二个'if'稍微好一些。 – asongtoruin

保持简短,简单而明确:

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

谢谢! (我太低级+1)。 – jdvcal

你可以使用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()))