2017-06-15 25 views
0

我正在爲輸入單詞搜索文本文件。但是,我只是想在文字「開始」之後搜索文件中的文本。 「開始」之前的第一個二十幾個應該被忽略。我知道如何查找「開始」,但不知道如何在遇到「開始」時搜索文件的其餘部分。我將不勝感激任何指導!如何在發現一個單詞時遍歷文件

這是我到目前爲止有:

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)包含你的文本文件的樣本以及你試圖在其中找到的東西。 –

回答

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發佈。我的答案的語法信用來自它的答案。

+0

謝謝,這是最好的解決方案! – jdvcal

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) 
這個

什麼?

+0

這是一個優雅的解決方案,但如果文件很大,應該逐行閱讀呢? – Ding

+0

該文件並不是那麼龐大,但我需要逐行讀取它才能完成作業...... – jdvcal

+0

謝謝!(我太低級+1)。 – jdvcal

-1

您可以使用一個布爾值:

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

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,請重置您的旗幟並開始尋找。

+0

這將在「開始」後的每一行打印'「對不起......」'直到找到該單詞。 – asongtoruin

+0

@ason​​gtoruin啊,謝謝你指出。重點是其他錯誤,沒有看到這一個。 –

+1

不用擔心 - 我在你之後發佈了幾乎相同的答案,但刪除了,因爲你對'continue'的使用比我使用的第二個'if'稍微好一些。 – asongtoruin

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

謝謝! (我太低級+1)。 – jdvcal

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