Tengo un archivo html con detalles de una persona, como el nombre, el apellido, la edad, el género.
Tengo un archivo python que toma los datos de estos archivos html y los guarda en algún archivo xyz.
Ahora quiero un botón de búsqueda, a través del cual si escribo algo en el cuadro de texto, se realiza una búsqueda en el archivo
mi archivo 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>
mi archivo 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()
Ahora, ¿qué debo escribir para hacer esta búsqueda en mi código de python? Me estoy quedando en blanco a qué escribir?
Respuestas
0 para la respuesta № 1Si lo que necesita es buscar en el archivo que acaba de crear una palabra:
with open("file.txt", "r") as f:
for line in f:
if "wordToLookFor" in line:
print "found it in : %s" % line