だから私はデータファイルを検索できるコードを作成しようとしています:
- 連絡先の詳細を取得して表示するための姓別
- 特定の月に誕生日を持つすべての連絡先を取得して表示するには、生年月日で検索します。
作成したコードは次のとおりです。
def search():
option = input('Please select to search by \n1. Surname\n2. D.O.B\n')
if option == '1':
surname = input('Please enter surname: ')
while not surname.isalpha():
surname = str(input('Please enter a valid surname: '))
Myfile = open('Address book.csv', 'rt')
for line in Myfile:
if ',' + str(surname) + ',' in line:
print(line)
else:
print('No contacts found')
elif option == '2':
Validmonth = False
while Validmonth == False:
month = input('Please enter the birth month')
if month >='13' and month <='0':
print('Please enter a valid month')
else:
Validmonth = True
Myfile = open ('Address book.csv', 'rt')
for line in Myfile:
if str(month) in line:
print(line)
else:
print('No contacts found')
else:
print('Error, select a valid option')
search()
search()
コードを試すと、次の結果が得られます。
Please select to search by
1. Surname
2. D.O.B
1
Please enter surname: Vickers
No contacts found
No contacts found
No contacts found
No contacts found
No contacts found
No contacts found
No contacts found
No contacts found
なぜ知りたいのですか?誰か助けてください?