If for all of them you split off the date and parse that as a datetime.datetime
then parse the time into a datetime.timedetla
and add it to the first value you should get where you need to be.
Parsing Python datetime from string with day-seconds
-
28-09-2022 - |
Вопрос
I'm trying to parse the date and time from a bunch of filenames that have one of these formats:
prefix.YYYY-MM-DD.suffix
prefix.YYYY-MM-DD_HH:MM:SS.sufix
prefix.YYYY-MM-DD-SSSSS.sufix
The datetime formats for these three are:
prefix.%Y-%m-%d.suffix
prefix.%Y-%m-%d_%H:%M:%S.suffix
prefix.%Y-%m-%d-%?????.suffix
The first two are easy to parse with the datetime module but I'm having trouble figuring out how to parse the 5-digit seconds which range from 00000 to 82800 (86400 seconds per day).
If at all possible, I'd like to use the standard datetime
module as this needs to be extremely portable.
My goal is to have a function that can ingest multiple datetime formats so I need to stay away from a one off parser if possible.
def myparser(filename, datetimeformat):
# do some stuff - maybe as easy as
datetimeobject = datetime.strptime(filename, datetimeformat)
return datetimeobject
Any thoughts on how best to do this would be greatly appreciated.
Решение
Не связан с StackOverflow