[英]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.