Question

In: Computer Science

using python3 Write a Python program that lets a user search through the 'data.txt' file for...

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.

  • Prompt the user for which field to search, (f) for first name, (l) for last name, or (e) for email
    • data.txt entries are all formatted as: first_name, last_name, email, separated by TABS
  • Your program should not read the entire file into memory, it should read line by line, and only keep/save lines that match the search.
  • Your program should ignore case (e.g. 'eric' and 'Eric' are the same name)
  • When finished, your program should print out all the search results, in a table format.

Test your program.


Solutions

Expert Solution

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]))


Related Solutions

Python: Write a program that asks the user for the name of a file. The program...
Python: Write a program that asks the user for the name of a file. The program should display the contents of the file line by line.
(write a program that get the numbers from user and search the file numbers.text for that...
(write a program that get the numbers from user and search the file numbers.text for that value. in C++) numbers.txt: 10 23 43 5 12 23 9 8 10 1 16 9 you must to have the exact output: Enter a number: 10 10 last appears in the file at position 9 Enter a number: 29 29 does not appear in the file Enter a number: 9 9 last appears in the file at position 12 Enter a number:
In Python write a program that prompts the user for a file name, make sure the...
In Python write a program that prompts the user for a file name, make sure the file exists and if it does reads through the file, count the number of times each word appears and then output the word count in a sorted order from high to low. The program should: Display a message stating its goal Prompt the user to enter a file name Check that the file can be opened and if not ask the user to try...
Question: Write a program in python that reads in the file climate_data_2017_numeric.csv and prompts the user...
Question: Write a program in python that reads in the file climate_data_2017_numeric.csv and prompts the user to enter the name of a field (other than Date), and then outputs the highest and lowest values recorded in that field for the month of August. The file climate_data_2017_numeric.csv contains the following fields: Date Minimum temperature (C) Maximum temperature (C) Rainfall (mm) Speed of maximum wind gust (km/h) 9am Temperature (C) 9am relative humidity (%) 3pm Temperature (C) 3pm relative humidity (%) Expected...
write a python program that will search through a range of launch angles (this will require...
write a python program that will search through a range of launch angles (this will require a loop statement) the target is 500 meters away and the projectile was launched at 100m/s, the initial height of the projectile can be between 2 and 30 meters the print statement will state the initial launch angle required to hit the target from the projectile height. ***explain each step or a rating will not be given***
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: You can set these constant global variables at the top outside of your main function definition: COMPUTER_WINS = 1 PLAYER_WINS = 2 TIE = 0 INVALID = 3 ROCK = 1 PAPER = 2 SCISSORS = 3 For this program 1 represents rock, 2 represents paper, and 3 represents scissors. In...
This needs to be a python3 code Write a program that prompts the user like this:...
This needs to be a python3 code Write a program that prompts the user like this: “Currency to convert to U.S. dollars: e = Euros, c= Chinese Yuan, r = Indian Rupees, b = Bitcoin: ”. Then depending on which letter the user enters, the program displays “Amount of Euros/Yuan/Rupees/Bitcoin to convert: ”. (Note: the second prompt should only name the one currency the user asked to convert, not all four currencies.) After the user enters the amount, the program...
In Python: Design a program that lets the user enter the total rainfall for each of...
In Python: Design a program that lets the user enter the total rainfall for each of the 12 months into a list. The program should calculate and display the total rainfall for the year, the average monthly rainfall, the months with the highest and lowest amounts. However, to start, do not ask the user for input. Instead, hard code the monthly rainfalls as a list in your program, and then use the list to determine how to conduct the processing...
Design a Python program with a While loop that lets the user enter a number. The...
Design a Python program with a While loop that lets the user enter a number. The number should be multiplied by 10 and the result stored in a variable named "product". The loop should repeat as long as the product variable < 10. Anything not completely obvious to a newbie should be commented on as in line comments. Should be modulated and repeatable as well as there should be an invocation of the main routine at the end of the...
In python please design a program that lets the user enter the total rainfall for each...
In python please design a program that lets the user enter the total rainfall for each of the last 10 years into an array. The program should calculate and display the total rainfall for the decade, the average yearly rainfall, and the years with the highest and lowest amounts. Should have Indentation Comments Good Variables initializations Good questions asking for the parameters of the code Output display with explanation Screenshot
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT