2013-10-21 196 views
0

假設特定領域我有一個名爲abc.txt文件,其中包含了以下數據。我是一個新手。所以,請大家幫忙搜索文本文件

Nathan Johnson 23 M 
Mary Kom  28 F 
John Keyman 32 M 
Edward Stella 35 M 

fnamelnameagegender與他們相關的指標?因爲我的要求是,如果用戶選擇通過名字搜索條件,應該在第一名稱列只搜索或選擇查詢條件如性別,它應該在性別搜索和顯示整個記錄。我該怎麼做呢?

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

回答

1

如果您想通過特定的信息進行搜索,您可以這樣做。所以說,用戶希望通過firstnameJohn進行搜索。這是你會怎麼做:

#!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 

如果用戶給了雙方一個firstnamegender,那麼你可以做這樣的事情:

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 

同時,始終記住關閉文件流,所以當你打電話open,你實際上是在腳本的末尾打開一個文件流,所以,總是關閉它們,像這樣f.close()。如果你使用with,那麼你並不需要擔心關閉。

0

更緊湊和靈活的解決方案,可以從標準庫使用namedtuple類型:

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) 

這樣,您就可以概括的搜索代碼。