0

我有一个 html 文件,其中包含一个人的详细信息,例如名字、姓氏、年龄、性别。
我有一个 python 文件,它从这些 html 文件中获取数据并将其保存在一些 xyz 文件中。
现在我想要一个搜索按钮,如果我在文本框中输入任何内容,通过它搜索文件

我的 index.html 文件:

<html>
<head>
<title>INFORMATION</title>
</head>
  <body>
    <form action = "/cgi-bin/test.py" method = "post">
    FirstName:
    <input type = "text" name = "firstname" /><br>
    LastName:
    <input type = "text" name = "lastname" /><br>
    <input type = "submit" name = "submit "value = "SUBMIT">
    <input type = "reset" name = "reset" value = "RESET">
    <input type = "button" name = "search" value = "SEARCH">
    </form>
 </body>
</html>

我的 test.py 文件:

#!/usr/bin/python
import cgi

def get_data():
    '''
This function writes the form data into the file 

    '''
    form = cgi.FieldStorage()

    Fname = form.getvalue('firstname')
    Lname = form.getvalue('lastname')
    Age = form.getvalue('age')
    Gender = form.getvalue('gender')
    f = open("/tmp/abc.txt","a")
    f.write(Fname + ' ' + Lname + ' ' + Age + ' ' + Gender + '\n')
    f.close()

    print "Content- type : text/html\n"
    print "Hello ", Fname, Lname, Age, Gender
    print "You have successsfully written your data into a file"

get_data()

现在我应该写什么来在我的python代码中进行这个搜索?我要写什么?

4

1 回答 1

0

如果您需要在刚刚创建的文件中搜索单词:

with open('file.txt', 'r') as f:
for line in f:
    if 'wordToLookFor' in line:
        print "found it in : %s" % line
于 2013-10-25T13:22:11.607 回答