Reputation: 328
I'm trying to get the expiry date out of string with a function as follows
p = 'Chiet TS Officer ~ ae\nOriginal Registration Date: 1998-06-17 ective Date: 2018-06-20\n\nLatest Revision Date: 2018-05-24 Expiry Date: 2021-06-19\nPage: 1 of 1\n\n \n\n.. making excellence a habit”\n\x0c'
def enddate(p):
for line in p.lower().split('\n'):
if "exp" in line:
try:
return(str(dparser.parse(line.split("exp")[-1],fuzzy=True))[0:10])
except:
try:
return(str(dparser.parse(line),fuzzy=True))[0:10]
except:
return(str(dparser.parse(line.split("20")[-1],fuzzy=True))[0:10])
elif "20" in line:
try:
return(str(dparser.parse(line.split("20")[-1],fuzzy=True))[0:10])
except:
return("error")
enddate(p)
'error'
you see that elif
bypassed if
, if I deleted the elif
part it would work well
p = 'Chiet TS Officer ~ ae\nOriginal Registration Date: 1998-06-17 ective Date: 2018-06-20\n\nLatest Revision Date: 2018-05-24 Expiry Date: 2021-06-19\nPage: 1 of 1\n\n \n\n.. making excellence a habit”\n\x0c'
def enddate(p):
for line in p.lower().split('\n'):
if "exp" in line:
try:
return(str(dparser.parse(line.split("exp")[-1],fuzzy=True))[0:10])
except:
try:
return(str(dparser.parse(line),fuzzy=True))[0:10]
except:
return(str(dparser.parse(line.split("20")[-1],fuzzy=True))[0:10])
enddate(p)
'2021-06-19'
so why does that happen? and how can I make my function look for "20"
only if all line
s don't contain "exp"
?
Upvotes: 0
Views: 96
Reputation: 753
Simply because "2018" is on the second line, while "expiry" is on the third. So in your case #2, first line nothing happens, second line you enter the elif condition and it returns the error. In case #1, nothing happens until the third line, and then you enter the fist if condition.
Upvotes: 2