私の abc txt ファイルは次のようになります。
Nathan Johnson 23 M
Mary Kom 28 F
John Keyman 32 M
Edward Stella 35 M
ここで、ファイル内の文字列を検索するときに、Mary を検索し、入力検索を "mary" とすると、データが見つかりません。つまり、大文字と小文字を区別しません。それ、どうやったら出来るの ?また、年齢を31として整数に変換すると、エラーが発生します
#!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 searchFName(self, matchString):
return matchString in self.fname
def searchLName(self, matchString):
return matchString in self.lname
def searchAge(self, matchString):
return matchString in self.age
def searchGender(self, matchString):
return matchString in self.gender
def display(self):
print self.fname, self.lname, self.age, self.gender
f= open("abc","r")
list_of_records = [Person(*line.split()) for line in f]
found = False
n=0
n1 = raw_input("Enter for Search Criteria\n1.FirstName 2.LastName 3.Age 4.Gender 5.Exit " )
if n1.isdigit():
n = int(n1)
else:
print "Enter Integer from given"
sys.exit(1)
if n == 0 or n>5:
print "Enter valid search "
if n == 1:
StringSearch = raw_input("Enter FirstName :")
for records in list_of_records:
if (records.searchFName(StringSearch)):
found = True
records.display()
if not found:
print "No matched record"
if n == 2:
StringSearch = raw_input("Enter LastName :")
for records in list_of_records:
if records.searchLName(StringSearch):
found = True
records.display()
if not found:
print "No matched record"
if n == 3:
StringSearch = int(raw_input("Enter Age :"))
if StringSearch > 100:
print "Please enter valid age"
for records in list_of_records:
if records.searchAge(StringSearch):
found = True
records.display()
if not found:
print "No matched record"
if n == 4:
StringSearch = raw_input("Enter Gender(M/F) :")
for records in list_of_records:
if records.searchGender(StringSearch):
found = True
records.display()
if not found:
print "No matched record"
if n == 5:
sys.exit(1)
エラー :
Enter for Search Criteria
1.FirstName 2.LastName 3.Age 4.Gender 5.Exit 3
Enter Age :41
Traceback (most recent call last):
File "TestObjectSearch.py", line 143, in <module>
if records.searchAge(StringSearch):
File "TestObjectSearch.py", line 77, in searchAge
return matchString in self.age
TypeError: 'in <string>' requires string as left operand