0

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

2 回答 2

1

You can do something like this, if you want to search via a particular piece of information. So say, the user wants to search through firstname for John. This is how you would do it:

#!usr/bin/python
import sys
class Records:
    def __init__(self, firstname, lastname, age, gender):
        self.fname = firstname
        self.lname = lastname
        self.age = age
        self.gender = gender

f= open("abc","r")
list_of_records = [Records(*line.split()) for line in f]
search_term = "John"
for record in list_of_records:
    if record.firstname == search_term:
        print "We found him!"
        print record.firstname, record.lastname, record.age, record.gender

If the user gave both a firstname and a gender, then you can do something like this:

for record in list_of_records:
    if record.firstname == "John" and record.gender = "M":
        print "We found him!"
        print record.firstname, record.lastname, record.age, record.gender

Also, always remember to close your file streams, so when you call open, you are actually opening a file stream, so at the end of the script, always close them, like so f.close(). If you use with, then you do not need to worry about closing.

于 2013-10-21T09:26:20.307 回答
0

For a more compact and flexible solution, you can use the namedtuple type from the standard library:

import collections

record_tuple = collections.namedtuple('Record',
                      ['Firstname', 'Lastname', 'Age', 'Gender'])

n = raw_input("Enter %s :" % ' '.join('%s.%s' % (i, name) for i, name in enumerate(record_tuple._fields, 1)))
n = int(n)

StringSearch = raw_input('Enter %s :' % record_tuple._fields[n-1])

for line in open('abc'):
    record = record_tuple(*line.split())
    if record[n-1] == StringSearch:
        print ' '.join(record)

This way, you can generalize the search code.

于 2013-10-21T10:30:10.023 回答