Kaeleigh has emailed you a CSV file with four columns: UBID, enrollment date, graduation…
- User
Kaeleigh has emailed you a CSV file with four columns: UBID, enrollment date, graduation date, and student type (either Full-Time or Part-Time). She has requested that you develop a program to filter the file in order to identify students who meet specific criteria. Specifically, you need to identify full-time students who took more than four years to graduate and part-time students who took more than seven years to graduate. The resulting CSV file should only include the students who meet these criteria. It should have the same columns as the original file, including the header row, and an additional column called "NumberOfYears."
Guideline
PLEASE READ CAREFULLY!!!
- Filter
input.csvto create a new file calledoutput.csv - Just take the difference between the year and do not worry about months.
- Submit the complete code and answer to each question as an HTML file converted from markdown. Just modify the attached markdown file.
- When grading, run the code to verify the output and then grade the answers to each question to assess if they make sense.
- You cannot use PANDAS or the CSV module. Please use basic python.
- Filter
- Assistant
To complete this task, you can follow the steps below:
- Read the
input.csvfile and store its content in a list calledcsv_data. - Create a function called
calculate_durationthat takes two years as input and returns the duration between them. - Create a filtered list called
filtered_databy applying the necessary conditions on the data. - Write the filtered data to a new file called
output.csvkeeping the same format as the input file and adding the "NumberOfYears" column.
- Read the
- User
this is the input UBID,EnrollmentDate,GraduationDate,Type 1111111,09-2001,10-2005,Full-Time 2222222,01-2004,10-2011,Part-Time 3333333,09-2011,05-2015,Full-Time 4444444,01-2011,10-2021,Part-Time 5555555,09-2003,05-2008,Full-Time 6666666,09-2004,10-2010,Part-Time 7777777,09-2002,05-2006,Full-Time 8888888,01-2004,10-2009,Part-Time 9999999,09-2005,05-2009,Full-Time
for this the above code is giving output 2222222,01-2004,10-2011,Part-Time,9 4444444,01-2011,10-2021,Part-Time,9 8888888,01-2004,10-2009,Part-Time,9
I want output as 4444444,01-2011,10-2021,Part-Time,10 5555555,09-2003,05-2008,Full-Time,5
fix the code accordingly
- Assistant
Apologies for the confusion earlier. It seems the condition for filtering students was not properly set. I have updated the filter_data function to take into account the exact duration for full-time and part-time students. Here's the updated code:
def read_csv_file(file_path): with open(file_path, "r") as file: content = file.readlines() return [line.strip().split(",") for line in content] def calculate_duration(enroll_year, grad_year): return int(grad_year) - int(enroll_year) def filter_data(data): filtered_data = [] headers = data[0] + ["NumberOfYears"] filtered_data.append(headers) for row in data[1:]: ubid, enroll_date, grad_date, student_type = row enroll_year = enroll_date.split("-")[0] grad_year = grad_date.split("-")[0] duration = calculate_duration(enroll_year, grad_year) if student_type == "Full-Time" and duration > 4: filtered_data.append(row + [str(duration)]) elif student_type == "Part-Time" and duration > 7: filtered_data.append(row + [str(duration)]) return filtered_data def write_to_csv_file(file_path, data): with open(file_path, "w") as file: for row in data: file.write(",".join(row) + "\n") csv_data = read_csv_file("input.csv") filtered_data = filter_data(csv_data) write_to_csv_file("output.csv", filtered_data)