jeudi 17 décembre 2020

Why Does 'elif' bypass 'if' in my python function [closed]

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 prevent it?

Aucun commentaire:

Enregistrer un commentaire