-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalendar Confusion.py
22 lines (17 loc) · 1.05 KB
/
Calendar Confusion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import re
def calendarize(line):
delim_regex = re.compile(r"\W")
date, cur_form, new_form = line.strip().split() # Pulls the strings
cur_delim = delim_regex.search(cur_form).group() # Finds the delimiter used in the date
new_delim = delim_regex.search(new_form).group() # Finds the delimiter to be used in output date
cur_date_tup = date.split(cur_delim) # Unpack the date by its delimiter into tuple
cur_form_tup = cur_form.split(cur_delim) # Unpack the current format by its delimiter into tuple
new_form_tup = new_form.split(new_delim) # Unpack the new format by its delimiter into tuple
new_date = []
for mdy in new_form_tup: # Locates the index of the required mm, dd, or yyyy and pulls its value from the date
new_date.append(cur_date_tup[cur_form_tup.index(mdy)])
print(new_delim.join(new_date)) # Prints the new date joined by the new delimiter
return new_delim.join(new_date)
with open("calendar.txt") as cal, open("out.txt", "w") as out:
for line in cal:
out.write(calendarize(line) + "\n")