I'm trying to check user input of a date/time in several allowable formats. (I know about the dateutil library. It's not what I'm looking for in this case.)
If some user input was accepted, the function must return a datetime object.
If ALL "try...except" fail — the function must return NONE. But I have 30-50 different date/time formats that I need to check.
I'm confused by the huge indentation in my code! How do I organize this format checking in a good style with GOOD performance?
# Test format check program
import datetime
def datetime_format_check(str):
try:
dt = datetime.datetime.strptime(str, "%y-%m-%d %H:%M")
return dt
except:
try:
dt = datetime.datetime.strptime(str, "%Y-%m-%d %H:%M")
return dt
except:
try:
dt = datetime.datetime.strptime(str, "%y-%m-%d")
return dt
except:
try:
dt = datetime.datetime.strptime(str, "%Y-%m-%d")
return dt
except:
try:
dt = datetime.datetime.strptime(str, "%H:%M")
return dt
except:
try:
# . . .
# many many try...except blocks )))
# . . .
return None # last except far far away from a screen border. ))))
while True:
str = input("Input date: ")
print("Result: ", datetime_format_check(str))
CodePudding user response:
Repetitive code? Well, that just begs to be replaced with a loop.
Put all of the formats in a list and iterate over it, checking each format:
def datetime_format_check(s):
formats = ["%y-%m-%d %H:%M", "%Y-%m-%d %H:%M", "%y-%m-%d"] # etc
for format in formats:
try:
dt = datetime.datetime.strptime(s, format)
return dt
except ValueError:
pass
return None
Some minor corrections I made to your code:
- Don't name your argument
str
; it shadows the builtin. - Don't use a bare
except:
, always catch the specific exception.