Kabir is a Python programmer working in a school. For the Annual Science Exhibition, he has created a csv file named Project.csv, to store the details of students who are participating in different science projects. The structure of Project.csv is :
[St_Id, St_Name, Project_Name, Project_Grade]
Where
-
St_Id is Student ID (integer)
-
ST_name is Student Name (string)
-
Project_Name is name of project in which student is participating(string)
-
Project_Grade is grade of the project which can be A, B or C (string)
For efficiently evaluating data of the exhibition, Kabir wants to write the following user defined functions:
Update() – to update the project grade of a student based on the student ID. The student ID and the new project grade should be accepted from the user.
findGrade(grade) – to display the names and project names of the students who have the given project grade.
As a Python expert, help him complete the task.
Answer:
Answer by student
Detailed answer by teachoo
- The question asks us to write two user defined functions, update() and findGrade(grade), to perform some operations on a csv file named Project.csv, that stores the details of students who are participating in different science projects. The structure of Project.csv is : [St_Id, St_Name, Project_Name, Project_Grade] Where St_Id is Student ID (integer) ST_name is Student Name (string) Project_Name is name of project in which student is participating(string) Project_Grade is grade of the project which can be A, B or C (string)
- The update() function should update the project grade of a student based on the student ID. The student ID and the new project grade should be accepted from the user.
- The findGrade(grade) function should display the names and project names of the students who have the given project grade.
-
To solve this problem, we need to perform the following steps:
- Import the csv module using import statement. This module allows us to read and write comma-separated values (csv) files.
-
Define our update() function using
def
statement. The function does not take any parameters.
- Open our file Project.csv in read mode using open() function with “r” as mode argument. This will allow us to read data from our file.
- Create a csv reader object using csv.reader() function with our file object as argument. This will allow us to iterate over each row in our file as a list of values.
- Create an empty list to store the updated records. We will use this list to write back to our file later.
- Take input from user for student ID and new project grade using input() function. We need to convert the student ID from string to integer using int() function, as we need to compare it with the student ID in our file, which is also an integer. We can store these inputs in variables st_id and new_grade respectively.
-
Loop through each row in our file using a
for
loop with our reader object as iterable. Each row represents a record of a student with their details as a list of values.
- Get the student ID and project grade from each row by indexing. The student ID is at index 0 and project grade is at index 3. We need to convert the student ID from string to integer using int() function, as we need to compare it with our user input, which is also an integer. We can store these values in variables sid and grade respectively.
- Check if the student ID matches our user input using == operator. If yes, then update the project grade with our user input by assigning new_grade to grade. If no, then keep the project grade as it is.
- Append the updated record to our records list using append() method. The updated record should be a list of four values: student ID, student name, project name and project grade. We can get the student name and project name from each row by indexing at index 1 and 2 respectively. We can use the sid and grade variables to store the updated student ID and project grade.
- Close our file using close() method. This will free up any resources used by our file object.
- Open our file again in write mode using open() function with “w” as mode argument. This will allow us to write data to our file. Note that this will overwrite any existing data in our file, so we need to make sure that we have stored all the updated records in our records list before doing this step.
- Create a csv writer object using csv.writer() function with our file object as argument. This will allow us to write rows of values to our file as comma-separated values.
-
Loop through each record in our records list using another
for
loop with our records list as iterable.
- Write each record to our file using writerow() method with each record as argument. This will write the list of values as a row of comma-separated values to our file.
- Close our file again using close() method.
- End our update() function definition using an empty line.
-
Define our findGrade(grade) function using
def
statement. The function takes one parameter, grade, which is the project grade to search for.
- Open our file Project.csv in read mode using open() function with “r” as mode argument. This will allow us to read data from our file.
- Create a csv reader object using csv.reader() function with our file object as argument. This will allow us to iterate over each row in our file as a list of values.
-
Loop through each row in our file using a
for
loop with our reader object as iterable. Each row represents a record of a student with their details as a list of values.
- Get the student name, project name and project grade from each row by indexing. The student name is at index 1, project name is at index 2 and project grade is at index 3. We can store these values in variables name, project and g respectively.
- Check if the project grade matches our parameter using == operator. If yes, then display the student name and project name using print() function. We can use string concatenation or formatting to include both values in one print statement.
- Close our file using close() method.
- End our findGrade(grade) function definition using an empty line.
Here is the code that implements these steps in Python: