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 readcsv It takes a string called fname as the parameter, representing the file name eg 'students.csv to read from. It should then read the contents of the file and return a list of dictionaries ie 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 keyvalue 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 floats representing the students scores.
The fourth entrys key is the string 'average' and its value is the average a float of the students scores, rounded to decimal places. You can use the round function to achieve this: roundx will round a number x to 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 ie has no content this function should return None.
If an errorexception occurs when opening the file eg 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': 'average':
name: 'Christa Maple', 'section': B 'scores': 'average':
name: 'Laurelle Cecil', 'section': B 'scores': 'average':
name: 'Ryanne Rusty', 'section': A 'scores': 'average':
Implement writecsv
Your second exercise is to write a function called writecsv It takes two parameters: a string fname representing the name of the CSV file to write to and studentdata which is a list of dictionaries as described in Section above. This function should write the contents of studentdata 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 where the number of rows is the number of students, and each row has 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 studentdata as shown in Section as the student data, then this function will write a file called output.csv with the following content:
Noa Marijus,A
Christa Maple,B
Laurelle Cecil,B
Ryanne Rusty,A
Implement filtersection point
Your next task is to write a function called filtersection. It takes two parameters: studentdata which is the list of dictionaries as described in Section and sectionname which is a string representing a section name egA 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 and A as the section name, it should return the following list of dictionaries:
name: 'Noa Marijus', 'section': A 'scores': 'average':
name: 'Ryanne Rusty', 'section': A 'scores': 'average':
Tip: this function can be easily implemented using list comprehension.
Implement filteraverage point
Now write a function called filteraverage. It takes three parameters: studentdata which is the list of dictionaries as described before, mininc representing a minimum value inclusive and maxexc 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 mininc, maxexc 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
