Eu tenho um arquivo html com detalhes de uma pessoa como nome, sobrenome, idade, sexo.
Eu tenho um arquivo python que leva os dados desses arquivos html e salva em algum arquivo xyz.
Agora eu quero um botão de pesquisa, através do qual se eu entrar em qualquer coisa na caixa de texto faz uma pesquisa através de arquivo
meu arquivo 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>
meu arquivo 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/htmln"
print "Hello ", Fname, Lname, Age, Gender
print "You have successsfully written your data into a file"
get_data()
Agora o que devo escrever para fazer essa pesquisa no meu código python? Eu estou indo em branco para o que escrever?
Respostas:
0 para resposta № 1Se o que você precisa é pesquisar no arquivo que você acabou de criar para uma palavra:
with open("file.txt", "r") as f:
for line in f:
if "wordToLookFor" in line:
print "found it in : %s" % line