Suppose I have a file named abc.txt
which contains the following data. I am an newbie. So please help
Nathan Johnson 23 M
Mary Kom 28 F
John Keyman 32 M
Edward Stella 35 M
Do fname
, lname
, age
, gender
have an index associated with them?? Because my requirement is that if the user select the search criteria through first name it should search in first name column only or select search criteria as gender, it should search in gender and display the whole record. What should I do further?
#!usr/bin/python
import sys
class Person:
def __init__(self, firstname=None, lastname=None, age=None, gender=None):
self.fname = firstname
self.lname = lastname
self.age = age
self.gender = gender
def display(self):
found = False
n1 = raw_input("Enter for Search Criteria\n1.FirstName == 2.LastName == 3.Age == 4.Gender : " )
print "Not a valid input"
if n1.isdigit():
n = int(n1)
else:
print "Enter Integer only"
if n == 0 or n>4:
print "Enter valid search "
if n == 1:
StringSearch = raw_input("Enter FirstName :")
for records in list_of_records:
if StringSearch in records.fname:
found = True
print records.fname, records.lname, records.age, records.gender
if not found:
print "No matched record"
if n == 2:
StringSearch = raw_input("Enter LastName :")
for records in list_of_records:
if StringSearch in records.lname:
found = True
print records.fname, records.lname, records.age, records.gender
if not found:
print "No matched record"
if n == 3:
StringSearch = raw_input("Enter Age :")
for records in list_of_records:
if StringSearch in records.age:
if not found:
print "No matched record"
if n == 4:
StringSearch = raw_input("Enter Gender(M/F) :")
for records in list_of_records:
if StringSearch in records.gender:
found = True
print records.fname, records.lname, records.age, records.gender
if not found:
print "No matched record"
f= open("abc","r")
list_of_records = [Person(*line.split()) for line in f]
#for record in list_of_records:
for per in list_of_records:
per.display()