[英]Searching a data in a file which takes the value from the html form and storing in some xyz file in python
我有一个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代码中进行搜索? 我对要写些什么一无所知?
如果您需要在刚刚创建的文件中搜索一个单词:
with open('file.txt', 'r') as f:
for line in f:
if 'wordToLookFor' in line:
print "found it in : %s" % line
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.