如何在一定条件下搜索文件中的数据

时间:2013-10-31 09:38:56

标签: python cgi

我在python中有一个cgi脚本,它在表单中进行搜索操作。表单包含FirstName,lastname,age和gender。

现在,如果用户输入年龄并点击男性,则应该在输入的字段中显示男性候选人而不是具有指定年龄的女性候选人。

我应该写什么条件

假设theres数据如

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

如果用户输入Age为“25”并在表格中选择“男性”,则应仅显示jacque和john详细信息

        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

如果我这样写,那么在表单上单击性别按钮时。它什么都不会显示

3 个答案:

答案 0 :(得分: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]

答案 1 :(得分: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

答案 2 :(得分:0)

if Fname == Fsearch and Lname == Lsearch and Age == Asearch and Gender == Gsearch:
    found = True