In: Computer Science
using python3
Write a Python program that lets a user search through the 'data.txt' file for a given first name, last name, or email address, and print all the matches/results.
Test your program.
query=input('Enter which files to search, (f) for first name,
(l) for last name, or (e) for email:')
if query.lower()=='f':
search=input('Enter a first name to search:
')
elif query.lower()=='l':
search=input('Enter a last name to search:
')
else:
search=input('Enter an email to search: ')
file='data.txt'
dataLines=[]
with open(file,'r') as f:
for line in f:
tokens=line.strip().split('\t')
if
query.lower()=='f':
if search.lower()==tokens[0].lower():
dataLines.append(tokens)
elif
query.lower()=='l':
if search.lower()==tokens[1].lower():
dataLines.append(tokens)
else:
if search.lower()==tokens[2].lower():
dataLines.append(tokens)
print('Search results: ')
print('{:^20}{:^20}{:>30}'.format('first name','last
name','email'))
for i in dataLines:
print('{:^20}{:^20}{:>30}'.format(i[0],i[1],i[2]))