2013-10-28 59 views
0

搜索的數據我有abcd.txt無法在一個文件在python

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

文件我有過這個文件中搜索search.py​​腳本。 這個search.py​​從html文件獲取值並在abcd.txt文件中進行搜索。 HTML有4個字段,分別是Firstname,Lastname,Age和Gender。當在姓名字段中輸入的數據是越來越搜查,但不是爲別人領域的沒有得到搜索

#!/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"    
    ''' 

get_search()

+0

請修復代碼的縮進。 –

+0

這與我使用的是一樣的。沒有縮進問題 –

+0

因此'Lname = form.getvalue('lastname')'是'get_search'的最後一行嗎?看起來並不適合我。 –

回答

0

你肯定爲LnameAgeGender值是輸入正確嗎? 其餘的對我來說很好。

+0

是值正確輸入 –

+0

你能否提供相關的html代碼? – Mailerdaimon

+0

否則我會嘗試重寫您的代碼使用[FieldStorage.Get列表](http://docs.python.org/2/library/cgi.html#cgi.FieldStorage.getlist) – Mailerdaimon

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()