0

我有一个文件 abcd.txt

Shashank  Saxena 24 male
Saurabh Parikh 23 male
Pradip Pillai 32 male

我有搜索此文件的 search.py​​ 脚本。这个 search.py​​ 从 html 文件中获取值并在 abcd.txt 文件中进行搜索。HTML有4个字段,分别是名字、姓氏、年龄和性别。当在名字字段中输入数据时,它正在被搜索,但对于其他字段,它不会被搜索

#!/usr/bin/python
import cgi

def get_search():


    found = False

    form = cgi.FieldStorage()
    Fname = form.getvalue('firstname')
    Lname = form.getvalue('lastname')
Age = form.getvalue('age')
Gender = form.getvalue('gender')

print "Content-type:text/html\n"

f = open("/tmp/abcd.txt","r")

for line in f:
    temp = line.split()
    #print temp
    Fsearch = temp[0]
    Lsearch = temp[1]
    Asearch = temp[2]
    Gsearch = temp[3]

    if Fname.lower() in Fsearch.lower():
        print line
        found = True

    if Lname.lower() in Lsearch.lower():
        print line
        found = True

    if Age in Asearch:
        print line
        found = True

    if Gender in Gsearch:
        print line
        found = True

if not found:
    print "No such Records"

    '''
    if Fname in temp or Lname in temp or Age in temp or Gender in temp:
        print "Hello", line
        found = True
if not found:
    print "No matched records"              
    '''

获取搜索()

4

2 回答 2

0
#!/usr/bin/python
import cgi

def get_search():

    found = False

    form = cgi.FieldStorage()
    Fname = form.getvalue('firstname', '')
    Lname = form.getvalue('lastname', '')
    Age = form.getvalue('age', 0)
    Gender = form.getvalue('gender', '')


    f = open("/tmp/abcd.txt","r")
    b = []
    for line in f:
        temp = line.split()

        Fsearch = temp[0]
        Lsearch = temp[1]
        Asearch = temp[2]
        Gsearch = temp[3]

        if Fname and Fname.lower() in Fsearch.lower():
            found = True

        if Lname and Lname.lower() in Lsearch.lower():
            found = True

        if Age and Age == Asearch:
            found = True

        if Gender and Gender in Gsearch:
            found = True
        if found:
            b.append(line)
        found = False
    f.close()
    print "Content-type:text/html\n"
    if len(b) == 0:
        print "No records found"
    else:
        for each in b:
             print each

get_search()
于 2013-10-29T06:23:13.577 回答
0

您确定 和 的值Lname输入Age正确Gender吗?其余的对我来说看起来不错。

于 2013-10-28T12:53:41.740 回答