Reputation: 15
I'm a school administrator trying to write up a simple program that will help us identify when a student has a student program end date that has expired and needs to be renewed. Here is a sample of a .txt file Python will read (fake student data and FERPA safe).
101321654RIEP*20211226IP13
101321654RIEP*20200215IP13
200500106RIEP*20200630IP13
The first 9 digits are the student number. So the first two lines are the same student. The third line is another student. The 8 digits after the asterisk is the program expiration date. So since the 1st student has a current program that is not expired, the second line is irrelevant and I need Python to disregard it (because the student's program was already renewed) and move onto the next student.
Where I'm struggling is in trying to tell Python "if you see the same student number as the previous line, skip over it and read the next one". Any guidance is much appreciated!
Upvotes: 0
Views: 69
Reputation: 1171
If your student list is not ordered by expiration date, a more robust solution would be to:
Note that I'm keeping the date as a string for simplicity purposes.
TODAY = "20210415"
with open(“your_file.txt”, “r”) as f:
student_number_to_latest_expiry_date = {}
for line in f:
student_number = line[:9]
expiry_date = line[-12:-4]
existing_expiry_date = student_number_to_latest_expiry_date.get(student_number, None)
if existing_expiry_date is None or expiry_date > existing_expiry_date:
student_number_to_latest_expiry_date[student_number] = expiry_date
for student_number, expiry_date in student_number_to_latest_expiry_date.items():
if expiry_date < TODAY:
print(f"need to renew for student #{student_number}")
Upvotes: 1