-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1.py
More file actions
72 lines (58 loc) · 2.01 KB
/
Copy path1.py
File metadata and controls
72 lines (58 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Problem 1
# Client data arrives with inconsistent date formats: "01-Jan-2024", "2024/01/01", "Jan 1 2024". Normalize all to ISO format.
from datetime import datetime
def normalize_dates(dates):
"""
Normalize dates in various formats to ISO format (YYYY-MM-DD).
Args:
dates: List of date strings in various formats
Returns:
List of dates in ISO format
"""
# Common date formats to try
date_formats = [
"%d-%b-%Y", # "01-Jan-2024"
"%Y/%m/%d", # "2024/01/01"
"%b %d %Y", # "Jan 01 2024"
"%B %d %Y", # "January 01 2024"
"%m/%d/%Y", # "01/01/2024"
"%d/%m/%Y", # "01/01/2024"
"%Y-%m-%d", # "2024-01-01"
]
normalized = []
for date_str in dates:
date_str = date_str.strip()
for fmt in date_formats:
try:
parsed_date = datetime.strptime(date_str, fmt)
iso_date = parsed_date.strftime("%Y-%m-%d")
normalized.append(iso_date)
break
except ValueError:
continue
else:
# If no format matches, try dateutil parser as fallback
try:
from dateutil import parser
parsed_date = parser.parse(date_str)
iso_date = parsed_date.strftime("%Y-%m-%d")
normalized.append(iso_date)
except Exception:
# If all parsing fails, append original and mark as failed
normalized.append(f"FAILED: {date_str}")
return normalized
# Test cases
if __name__ == "__main__":
test_dates = [
"01-Jan-2024",
"2024/01/01",
"Jan 1 2024",
"January 15 2024",
"01/15/2024",
"2024-03-20",
]
results = normalize_dates(test_dates)
print("Date Normalization Results:")
print("-" * 50)
for original, normalized in zip(test_dates, results):
print(f"{original:20} -> {normalized}")