2011-11-15 7 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

私はあなたのコードを実行することはできませんので、便利な3つのpythonを持っていませんが、あなたの質問は何ですか?コードは実行されていますか?ほとんど走っている?トレースバックエラーや? –

答えて

0

whileループから各行を参照するとしますが、プログラムにはwhileループがありません。

また、あなたの変数hitsの目的や変数i何(理由変数が?)

0

私はあなたのコードにいくつかの編集を行いました。私は「そのように」で分割ラインに完全なファイルを読むために)(私は、私はその後、readlinesを行うように変更raw_inputするための入力を変更し、将来輸入print_function

から追加

をのpython 2.6を使用していますあなたはそれぞれのキャラクターに分割されません。

私はあなたがここで何を考えている見当がつかない:URL = infile.readline()

あなたの出力を使用すると、URLを印刷したいが、あなたは、入力用のテキストファイルを持って示しており、それは加算されませんあなたは本当にそこに欲しいものを考えてください。

質問を洗練してください。

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