关键词 > Python代写

This project lets you practice working with CSV files in Python.

发布时间:2023-11-28

Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

About

This project lets you practice working with CSV files in Python. On top of reinforcing your knowledge and skills about working with les, you will continue to practice your existing skill sets including defi ning functions, evaluating expressions, formatting strings, working with data types such as lists, dictionaries, strings and nested data types, handling exceptions, and problem solving using loops and branching.

Learning Objectives

1.   Practice working with CSV files in Python.

2.   Practice working with dictionaries, lists, nested data types, and loops.

3.   Practice exception handling using try-except statements.

Estimated Size

1 Python file. 6 functions, some of which build upon other functions you have implemented.

0. Setup

Download the starter code provided for this project. It’s a zip that contains two les: a python program csv_scores.py (this is the only file you need to work on and submit) and an example CSV file students.csv. Unzip this file to a folder on your computer. To understand the content of students.csv, open it in VSCode or any text editor. Note that each row represents a record  for a unique student. Each row has 12 values (separated by commas) that correspond to 12 different fie lds: student name, section, score1, score2, ..., score10. In a typical CSV file, the fi rst row is usually the column titles. For simplicity, we have omitted that row, so the file   content starts straight with the data.

Note that the students.csv provided in the starter code is only an example. When you submit your code to Gradescope, it will use a different CSV file for grading, so you must test your functions rigorously. Feel free to modify the example students.csv to facilitate your own testing.

At the top of the program file csv_scores.py, include your name, email, and SPIRE ID as comments, like in the example given below. Please note that NO group submission is allowed, therefore do NOT include any other students’ information. All programming work here must be yours and yours alone.

# Author : John Snow

# Email : j [email protected]du

# Spire ID : 12345678

1. Implement read_csv (20 points)

Your fi rst 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 fi rst entry’s key is the string  'name' and its value is the student’s name.

The second entry’s key is the string  'section' and its value is the student’s section name.

●   The third entry’s key is the string  'scores' and its value is a list of 10 floats representing the student’s 10 scores.

●   The fourth entry’s key is the string  'average' and its value is the average (a float) of the student’s 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 (20 point)

Your second exercise is to write a function called write_csv. It takes two parameters: a string f name (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 lters 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.

For example, if we call this function with the example student data as shown in Section 1, 80 as min_inc and 85 as max_exc, it will return the following list of dictionaries:

[{'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': '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}]

5. Implement split_section (15 points)

Now you will write a function called split_section. Its job is to take an input CSV file, and split it into multiple les, one for each section. Each le should contain only records of students belonging to that section. Note that you must deduce the set of section names from the input     CSV file, you may NOT assume the number of sections is a fixed number, and the section names may NOT always be named  'A', 'B', 'C' etc.

This function takes a string fname as the parameter, representing the input CSV file that contains the original student records. It should then write multiple output les, one for each unique section that exists in the input le. Your function can be implemented as follows:

●   Call read_csv function that you’ve implemented in Section 1 to read the input CSV file into a list of dictionaries. Remember that read_csv performs some error handling. So if read_csv returns None, this function should return immediately without further processing.

●    Deduce/extract the set of unique section names. As explained above, you may NOT hardcode the section names in your program because they are not known a priori. Instead, consider using set comprehension to help you figure out the set of unique section names.

●    For each section name in the set, call the filter_section function you’ve already implemented to get data of students in that section only, then call the write_csv function you’ve implemented to write the data out to its specific section le. The name of each section file should be formatted as: INPUTFILE_section_SECTIONNAME.csv where INPUTFILE is the base name of the input file (i.e. without extension name .csv), and SECTIONNAME is the name of a section. For example, if the input file name is cics110.csv, and in it there is a section named NFT, then data of all students belonging to this section    should be written into a file named c ics110_section_NFT.csv

If we call this function with students.csv in the starter code as the argument, it should write multiple les, one of which is students_section_A.csv that should have the following content. You can similarly work out the files for the other sections.

Noa Marijus,A,91.4,82.53,86.52,84.7,81.69,84.11,86.29,91.63,92.42,94.7

Ryanne Rusty,A,96.2,80.3,88.78,68.53,88.3,73.72,74.44,79.22,78.09,75.32

6. Implement get_assignment_stats (15 points)

Your fi nal exercise is to write a function called get_assignment_stats. It will calculate some statistics based on the student scores in each assignment (from 1 to 10), as well as on the average scores. This function takes one parameter: student_data which is the list of dictionaries as described previously. It returns a new list of dictionaries, where each element in this list is a dictionary containing the mean, standard deviation, min, max, and range of a grading item. In the following we refer to this as the return_list. There are a total of 11 grading items:  the average score, and individual scores 1 through 10. Specifically, your function should:

1.   First, collect every students’ average score (i.e. the  'average' fie ld in student_data) into a list, let’s call this list nums. Consider using list comprehension to help you create this list.

The length of this list should be equal to the number of students. Then, using this list, compute the following statistics. You should consider writing a function (say, call it get_stats) to calculate these statistics as you will need them again in the following steps.

mean = sum(nums) / len(nums)

minimum = min(nums)

maximum = max(nums)

range = maximum - minimum

std_dev = (sum([(n - mean)**2 for n in nums]) / len(nums)) ** (1/2)

2.   Create a dictionary that has 5 key-value pairs (entries). The keys of these entries are named 'mean', 'std_dev', 'min', 'max', 'range' respectively, and the value is each key’s corresponding value calculated above. This dictionary will be the rst element (i.e. at index 0) of return_list.

3.  Next, collect every students’ score1 (i.e. fi rst number in the  'scores' fie ld in student_data) into a list. Again, consider using list comprehension to create this list. Then  calculate the same statistics as Step 1 above, and create a new dictionary of the 5 key-value pairs. Append this dictionary to return_list, so that it becomes the second element (at index 1) of return_list.

4.  Repeat this for the other scores (2, 3 through 10), and the dictionary of statistics you create for each score should be appended one-by-one to return_list. Obviously as doing so for  each score is very repetitive, you should consider using a loop instead of writing code for each score individually.

5.  Finally return the list.

In summary, return_list should contain exactly 11 elements: element at index 0 being the dictionary of statistics calculated on the average score; element at index 1 being the dictionary of statistics calculated on score1; element at index 2 being statistics calculated on score2, and  so on and so forth.

If we call this function with examples student_data in Section 1 as input argument, it should return the following list of dictionaries:

[{'mean': 84.03825, 'std_dev': 3.702226179949031, 'min': 80.29, 'max': 87.879,

'range': 7.588999999999999},

{'mean': 94.3425, 'std_dev': 2.502382614629505, 'min': 91.4, 'max': 97.37,

'range': 5.969999999999999},

{'mean': 81.985, 'std_dev': 1.7020061692015118, 'min': 80.3, 'max': 84.53,

'range': 4.230000000000004},

{'mean': 87.65, 'std_dev': 1.1300000000000026, 'min': 86.52, 'max': 88.78,

'range': 2.260000000000005},

{'mean': 76.61500000000001, 'std_dev': 8.085, 'min': 68.53, 'max': 84.7, 'range':

16.17},

{'mean': 84.995, 'std_dev': 3.3049999999999997, 'min': 81.69, 'max': 88.3,

'range': 6.609999999999999},

{'mean': 78.91499999999999, 'std_dev': 5.195, 'min': 73.72, 'max': 84.11, 'range':

10.39},

{'mean': 80.36500000000001, 'std_dev': 5.925000000000004, 'min': 74.44, 'max':

86.29, 'range': 11.850000000000009},

{'mean': 85.42500000000001, 'std_dev': 6.204999999999998, 'min': 79.22, 'max':

91.63, 'range': 12.409999999999997},

{'mean': 85.255, 'std_dev': 7.164999999999999, 'min': 78.09, 'max': 92.42,

'range': 14.329999999999998},

{'mean': 84.835, 'std_dev': 9.76685594242078, 'min': 74.82, 'max': 94.7, 'range':

19.88000000000001}]

(Common Paragraph) Submit to Gradescope, Correct mistakes

Submit your program csv_scores.py to gradescope. Wait for the autograder and review failed tests if any. Make the appropriate changes in your code and resubmit as many times as you need before the deadline. While working on each question, you can submit a partially fi nished program and receive feedback. Your highest scoring submission will be recorded as your grade. Note that each failed test includes text at the bottom giving hints as to why that test failed.