搜索文件中的数据,该文件从html表单获取值并存储在python中的某个xyz文件中

时间:2013-10-25 13:04:31

标签: python html cgi

我有一个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代码中进行搜索?我要写什么空白?

1 个答案:

答案 0 :(得分:0)

如果你需要的是在你刚创建的文件中搜索一个单词:

with open('file.txt', 'r') as f:
for line in f:
    if 'wordToLookFor' in line:
        print "found it in : %s" % line