from dateutil.parser import parse
import re
from datetime import datetime
def date_fun(date):
try:
dt = parse(date)
dt = dt.strftime('%d/%m/%Y')
dmy_split=re.split('[- / ]', dt)
# error here:
if (eval(dmy_split[0])>=1 and eval(dmy_split[0])<=12 and eval(dmy_split[1])>=1 and eval(dmy_split[1])<=12 or len(dmy_split[2])==2):
print("ambiguous") # Format matched
else:
print("True")
except ValueError: # If match not found keep searching
print("False")
date_fun("abc")
date_fun("12/1/91")
date_fun("2002-9-18")
This a date validation program and I got the 'Leading 0 not permitted' error at the highlighted part though I haven't used '01' anywhere:
if (eval(dmy_split[0])>=1 and eval(dmy_split[0])<=12 and eval(dmy_split[1])>=1 and eval(dmy_split[1])<=12 or len(dmy_split[2])==2):
File "<string>", line 1
01
^
SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers
Why am I getting this error, and how do I get rid of it?
though I haven't used '01' anywhere
Yes you have. The first element in dmy_split
is '01'
.
This is one of the many reasons why eval()
is not recommended.
Why do you need eval()
anyway? Just use int()
.