Is there an equivalent to C#'s DateTime.TryParse()
in Python?
I'm referring to the fact that it avoids throwing an exception, not the fact that it guesses the format.
Is there an equivalent to C#'s DateTime.TryParse()
in Python?
I'm referring to the fact that it avoids throwing an exception, not the fact that it guesses the format.
How about strptime?
http://docs.python.org/library/time.html#time.strptime
It will throw a ValueError if it is unable to parse the string based on the format that is provided.
Edit:
Since the question was edited to include the bit about exceptions after I answered it. I wanted to add a note about that.
As was pointed out in other answers, if you don't want your program to raise an exception, you can simply catch it and handle it:
That's a Pythonic way to do what you want.