मेरे पास एक HTML फ़ाइल है जिसमें किसी व्यक्ति का विवरण है जैसे प्रथम नाम, अंतिम नाम, आयु, लिंग।
मेरे पास एक पायथन फ़ाइल है जो इन 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/htmln"
print "Hello ", Fname, Lname, Age, Gender
print "You have successsfully written your data into a file"
get_data()
अब इस खोज को मेरे पायथन कोड में बनाने के लिए मुझे क्या लिखना चाहिए? मैं लिखने के लिए खाली जा रहा हूँ क्या?
उत्तर:
जवाब के लिए 0 № 1यदि आपको जो चाहिए वह उस फ़ाइल में खोज रहा है जिसे आपने अभी एक शब्द के लिए बनाया है:
with open("file.txt", "r") as f:
for line in f:
if "wordToLookFor" in line:
print "found it in : %s" % line