2011-11-15 139 views
-3

我試圖引用其中出現多次關鍵字的行,我需要能夠引用while循環中的每一行。此外,我需要大膽的關鍵詞在行中。謝謝!在while循環中爲每個循環分配新變量

def main(): 
    print("The Great Search Engine, by Eric Santos") 
    # input from user name of database file 
    dname = input("Enter name of database file: ") 
    # input from user seach term to look for 
    term = input("Enter keyword to search for: ") 
    # open html file for writing 
    outfile = open("mypage.html", "w") 

    # open database file for reading 
    infile = open(dname,"r") 
    # for each line in the file: 
    hits=0 
    i = 0 
    for line in infile: 
     listword = line.split() 
     for word in listword: 
      if term == word: 
       print(word) 
       print(line) 
       # read in URL line 
       url = infile.readline() 
       print(url) 
       hits=hits+1 
       i=i+1 

    if hits==1: 
     print ("Keyword", term, "found", hits,"times") 
     print("<html>", file=outfile) 
     print("<head><title>Search Findings</title></head>", file=outfile) 
     print("<body>", file=outfile) 
     print('<h2><p align=center>Search for "', term, '"</h2>"', file=outfile) 
     print("<p align=center>", file=outfile) 
     print("<table border>", file=outfile) 
     print("<tr><th> Hits <th>URL</tr>", file=outfile) 
     print("<html>", file=outfile) 
     print("<tr><td>",line,"<td><a href=", url,"> ",url,"</a></tr>", file=outfile) 
     print("</table>", file=outfile) 
     print("</body>", file=outfile) 
     print("</html>", file=outfile) 

    if hits ==2: 
     print ("Keyword", term, "found", hits,"times") 
     print("<html>", file=outfile) 
     print("<head><title>Search Findings</title></head>", file=outfile) 
     print("<body>", file=outfile) 
     print('<h2><p align=center>Search for "', term, '"</h2>"', file=outfile) 
     print("<p align=center>", file=outfile) 
     print("<table border>", file=outfile) 
     print("<tr><th> Hits <th>URL</tr>", file=outfile) 
     print("<html>", file=outfile) 
     print("<tr><td>search <b>engine</b><td><a href=", url,"> ",url,"</a></tr>", file=outfile) 
     print("<tr><td>search <b>engine</b><td><a href=", url,"> ",url,"</a></tr>", file=outfile) 
     print("</table>", file=outfile) 
     print("</body>", file=outfile) 
     print("</html>", file=outfile) 

     outfile.close() 

    if hits == 0: 
     print("Keyword", term,"not found") 

     # use find to see if search term is in line 
     # if find is successful 
      # add one to the hits 
      # output the file to the html file 
    # if after count still zero 
     # show to shell no hits 
     # output to html file "no hits" 
    # else 
     # output to shell how many hits 
    infile.close() 

main() 

# write out the end of html to the html file 
# close file 
+0

「while」循環在哪裏? –

+0

我沒有python 3方便,所以我不能運行你的代碼,但是你的問題是什麼?代碼是否正在運行?幾乎跑?產生追蹤錯誤或? –

回答

0

你說你想引用while循環中的每一行......但是你的程序中沒有while循環。

而且什麼是你的變量hits的目的和可變i(爲什麼變量?)

0

我做了一些修改你的代碼。我使用的Python 2.6,所以我改變輸入和的raw_input從未來進口print_function

我再改做readlines方法添加()來讀取完整的文件到線

我的「分裂」,使你不會分裂每個角色。

我不知道你在想什麼位置:URL = infile.readline()

你的輸出指示要打印一個網址,但你必須輸入的文本文件,並且不加起來到一個網址,所以想想你真的想在那裏。

看一看,也許改善你的問題。

from __future__ import print_function 

def main(): 
    print("The Great Search Engine, by Eric Santos") 
    # input from user name of database file 
    dname = raw_input("Enter name of database file: ") 
    # input from user seach term to look for 
    term = raw_input("Enter keyword to search for: ") 
    # open html file for writing 
    outfile = open("mypage.html", "w") 

    # open database file for reading 
    infile = open(dname,"r") 
    # for each line in the file: 
    hits=0 
    i = 0 
    for line in infile.readlines(): 
    print(line) 
    listword = line.split(" ") 
    print(listword) 
    for word in listword: 
     if term == word: 
      print(word) 
      print(line) 
      # read in URL line 
      url = infile.readline() 
      print(url) 
      hits=hits+1 
      i=i+1