Question: Your first exercise is to write a function called read _ csv . It takes a string called fname as the parameter, representing the file

Your first exercise is to write a function called read_csv. It takes a string called fname as the parameter, representing the file name (e.g., 'students.csv') to read from. It should then read the contents of the file and return a list of dictionaries (i.e. student data) as described below:
The length of the list should be equal to the number of students.
Each element of the list is a dictionary with 4 key-value pairs (entries).
The first entrys key is the string 'name' and its value is the students name.
The second entrys key is the string 'section' and its value is the students section name.
The third entrys key is the string 'scores' and its value is a list of 10 floats representing the students 10 scores.
The fourth entrys key is the string 'average' and its value is the average (a float) of the students 10 scores, rounded to 3 decimal places. You can use the round() function to achieve this: round(x,3) will round a number x to 3 decimal places. Note that this average value is not part of the CSV file and therefore you must calculate this on your own.
In addition, you function must handle the following cases:
If the file is empty (i.e. has no content), this function should return None.
If an error/exception occurs when opening the file (e.g. due to file not existing), the function should print to the screen an error message 'Error occurred when opening {fname} to read', where {fname} is the actual name of the file, and then return None.
A correct implementation will result in the following returned student data for the given students.csv from the starter code:
[{'name': 'Noa Marijus', 'section': 'A', 'scores': [91.4,82.53,86.52,84.7,81.69,84.11,86.29,91.63,92.42,94.7], 'average': 87.599},
{'name': 'Christa Maple', 'section': 'B', 'scores': [97.37,80.58,88.78,68.53,88.3,73.72,74.44,79.22,78.09,74.82], 'average': 80.385},
{'name': 'Laurelle Cecil', 'section': 'B', 'scores': [92.4,84.53,86.52,84.7,81.69,84.11,86.29,91.63,92.42,94.5], 'average': 87.879},
{'name': 'Ryanne Rusty', 'section': 'A', 'scores': [96.2,80.3,88.78,68.53,88.3,73.72,74.44,79.22,78.09,75.32], 'average': 80.29}]
2. Implement write_csv
Your second exercise is to write a function called write_csv. It takes two parameters: a string fname (representing the name of the CSV file to write to), and student_data which is a list of dictionaries as described in Section 1 above. This function should write the contents of student_data to the file named fname except the average score of every student. In other words, the file that this function generates has the exact format as described in Section 0, where the number of rows is the number of students, and each row has 12 values.
If an exception occurs when opening the file, the function should print to screen the error message 'Error occurred when opening {fname} to write', where {fname} is the actual name of the file. Then the function should return without further processing.
For example, if we call this function with 'output.csv' as the file name and the example student_data as shown in Section 1 as the student data, then this function will write a file called output.csv with the following content:
Noa Marijus,A,91.4,82.53,86.52,84.7,81.69,84.11,86.29,91.63,92.42,94.7
Christa Maple,B,97.37,80.58,88.78,68.53,88.3,73.72,74.44,79.22,78.09,74.82
Laurelle Cecil,B,92.4,84.53,86.52,84.7,81.69,84.11,86.29,91.63,92.42,94.5
Ryanne Rusty,A,96.2,80.3,88.78,68.53,88.3,73.72,74.44,79.22,78.09,75.32
3. Implement filter_section (15 point)
Your next task is to write a function called filter_section. It takes two parameters: student_data which is the list of dictionaries as described in Section 1, and section_name which is a string representing a section name (e.g.,'A'). It should return a new list of dictionaries which only contains data of students who are in the specified section. In other words, it filters out students that are not in the specified section.
For example, if we call this function with the example student data as shown in Section 1, and 'A' as the section name, it should return the following list of dictionaries:
[{'name': 'Noa Marijus', 'section': 'A', 'scores': [91.4,82.53,86.52,84.7,81.69,84.11,86.29,91.63,92.42,94.7], 'average': 87.599},
{'name': 'Ryanne Rusty', 'section': 'A', 'scores': [96.2,80.3,88.78,68.53,88.3,73.72,74.44,79.22,78.09,75.32], 'average': 80.29}]
Tip: this function can be easily implemented using list comprehension.
4. Implement filter_average (15 point)
Now write a function called filter_average. It takes three parameters: student_data which is the list of dictionaries as described before, min_inc representing a minimum value (inclusive), and max_exc representing a maximum value (exclusive). It should return a new list of dictionaries which only contains data of students whose average score is within the specified range between [min_inc, max_exc). Again, this function can be easily implemented using list comprehension.
python languag

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!