[Tutor] (no subject)
Matthew Herzog
matthew.herzog at gmail.com
Tue Mar 26 14:26:09 EDT 2019
elif fileDate = datetime.strptime(name[0:8], DATEFMT).date():
^
SyntaxError: invalid syntax
On Sat, Mar 23, 2019 at 6:43 PM Mats Wichmann <mats at wichmann.us> wrote:
> On 3/23/19 3:16 AM, Peter Otten wrote:
>
> > Personally I would use a try...except clause because with that you can
> > handle invalid dates like 99999999_etc.xls gracefully. So
> >
> > ERASED = "erased_"
> >
> >
> > def strip_prefix(name):
> > if name.startswith(ERASED):
> > name = name[len(ERASED):]
> > return name
> >
> >
> > def extract_date(name):
> > datestr = strip_prefix(name)[:8]
> > return datetime.datetime.strptime(datestr, DATEFMT).date()
> >
> >
> > for name in ...:
> > try:
> > file_date = extract_date(name)
> > except ValueError:
> > pass
> > else:
> > print(file_date)
>
> I'd endorse this approach as well.. with a DATEFMT of "%Y%m%d", you will
> get a ValueError for every date string that is not a valid date... which
> you then just ignore (the "pass"), since that's what you wanted to do.
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
--
The plural of anecdote is not "data."
More information about the Tutor
mailing list