Question: Define a function named read_students(filename)which takes a filename as a parameter, reads and returns a list of tuples. The name of the text file to
Define a function named read_students(filename)which takes a filename as a parameter, reads and returns a list of tuples. The name of the text file to be processed is specified in the filename parameter. The text file contains 1 or more lines of student data. Each line consists of a student name, student id and an email address. Each data item in a line is separated by a comma (",").
The function reads each line of student data in the file and returns a list of student tuples. Each tuple will contain a student name, student id and an email address. The list should be sorted by name in alphabetical ascending order. Remember to close the input file.
Click here to download a sample text file.
For example:
| Test | Result |
|---|---|
print(read_students("classlist3.txt")) | [('John', 'N00000001', 'john@amail.com'), ('Kelly', 'N00000002', 'kelly@bmail.com'), ('Nicky', 'N00000003', 'nicky@cmail.com')] |
result = read_students("classlist1.txt") if not isinstance(result[0], tuple): print("The function must return a list of tuples") print(result) | [('John', 'N00000001', 'john@amail.com')] |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
