为while循环中的每个循环分配新变量

时间:2011-11-15 02:00:37

标签: python variables loops while-loop

我试图引用一个关键字出现的行,如果它出现多次,我需要能够引用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

2 个答案:

答案 0 :(得分:0)

你说你想引用while循环中的每一行......但你的程序中没有while循环。

您的变量hits和变量i(为什么两个变量?)

的目的是什么?

答案 1 :(得分:0)

我对您的代码进行了一些修改。我正在使用python 2.6所以我将输入更改为raw_input并添加了 future import 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