2013-10-31 74 views
1

我在python中有一個cgi腳本,它可以在窗體中進行搜索操作。表格具有名字,姓氏,年齡和性別。如何在特定條件下搜索文件中的數據

現在,如果用戶輸入年齡並點擊男性,則應在輸入的字段中顯示男性候選人而不是指定年齡的女性候選人。

我應該什麼條件寫這樣

john duck 25 male 
sashi tharoor 34 male 
jacque kalis 25 male 
amanda seyfried 25 female 

假設那裏有數據,如果用戶輸入年齡爲「25」,並選擇在表單中的「男性」,它應該顯示雅克和約翰的細節只有

 if Fname.lower() == temp[0]: 
      found = True 

     if Lname.lower() == temp[1]: 
      found = True 

     #if Age and Age == temp[2]: 
      #found = True 

     #if Gender and Gender == temp[3] : 
      #found = True 

     if Age == temp[2] and Gender == temp[3]: 
      found = True 

如果我這樣寫,比在表單上單擊性按鈕時。它會顯示什麼

+0

這個問題是幾乎與你的其他[問題]一樣(http://stackoverflow.com/questions/19699621/entering-two-field-data-and-making-a-search-if-satisfies-in-cgi-script-of- python)所以我在這裏添加此鏈接以供參考 – Mailerdaimon

回答

0

您可以'keyword' in 'text'

對於實例測試:

line = 'john duck 25 male' 

'john' in line 
> True 

'john' and 'duck' in line 
> True 

'john' and 'female' in line 
> False 

要檢查是否所有輸入的值都是有效的,你可以做這樣的事情

#genderEntered should be True if something was entered and False otherwise 
if genderEntered == True: 
    genderCorrect = gender in line 

#do this for all fields 

#all correct? 
if genderCorrect and nameCorrect: 
    #all values are correct 
else: 
    #at least one was not correct 
+0

只是將此與@Naster的答案結合起來,您應該很好 – Mailerdaimon

1
with open('data.txt') as f: 
    line = f.read().split('\n') 
    sex = 'male' 
    age = '25' 
    for l in line: 
    tmp = l.split(' ') 
    if sex == tmp[3] and age == tmp[2]: 
     print tmp[0], tmp[1] 
+0

已更新的代碼和以下問題..... –

0
if Fname == Fsearch and Lname == Lsearch and Age == Asearch and Gender == Gsearch: 
    found = True 
相關問題