简体   繁体   中英

Searching a data in a file which takes the value from the html form and storing in some xyz file in python

I have an html file with detail of an person such as firstname, lastname, age, gender.
I have an python file which takes the data from these html file and saves it in some xyz file.
Now i want a search button, through which if i enter anything in the textbox makes a search through file

my index.html file:

<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>

my test.py file:

#!/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()

Now what should i write to make this search in my python code? I am going blank to what to write ?

if what you need is searching in the file you just created for a word :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM