Write a program which does the following: (a) Takes in a CSV file, where each li
ID: 3843920 • Letter: W
Question
Write a program which does the following: (a) Takes in a CSV file, where each line of the file contains: , , (b) Asks for the user to input a particular grade to search by (c) Searches for all entries in the CSV(Comma Separated Values) which contains a grade greater than the given grade (d) Prints to screen all names(first last) that match the criteria For example, given a CSV file labeled students.txt: Oppenheimer, Robert, 80 Fermi, Enrico, 90 Feynman, Richard, 70 Teller, Edward, 60 Frisch, Otto, 50 lt the user enters the grade 75, the resulting output of the program would be: The following people have a better grade than 75: Robert Oppenheimer Enrico FermiExplanation / Answer
import csv # # Uncomment this if you really want to work with csv file
lines = list() # For storing each record in a line
with open('students.txt','r') as jankari:
f = csv.reader(jankari) # CSV reader reads file in form of list line by line
for i in f:
lines.append(i[0]+','+i[1]+','+i[2]) # Separated info on one line with , (here we know that file has 3 columns)
# print(lines) ## Sanity check
s_fname = input('>Enter First name : ')
s_lname = input('>Enter last name : ')
s_grade = input('>Enter grade : ')
toppers = list() # Obviously for toppers
for line in lines:
lname, fname, grade = line.split(',') # unpacks 3 parameters at once
if grade > s_grade:
toppers.append(fname+' '+lname) # we got the toppers
if toppers:
print('The following people have grades higher than {0}'.format(s_grade))
print(' '.join(toppers)) # Finally here are the toppers
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.