Find invalid value in date field

I have the text file with column Date and it also have some wrong data in date column like space and characters ABC. I just want to maintain those records which have characters into the different file named as Error_file.txt.

Tried:

import dateutil.parser as dparser
from dateutil.parser import parse

datelist = ["11/Jan/2018 01:01:00", "" ,"01/01/17", "2016-01-02","KK"]

def is_date(string):
    try:
        parse(string)
        return True
    except ValueError:
        return False

with open('output.txt', 'w') as output:
    with open('error_file.txt', 'w') as error:
        for i in datelist:
            if is_date(str(datelist)) == True:
                #WRITE TO Output File
                print dparser.parse(i, fuzzy=True).date()
            else:
                #WRITE TO Error File
                print('Error line')

In the above given, record with KK should be maintained in Error_file.txt.

But getting following output:

Error line
Error line
Error line
Error line 
Error line

2 answers

  • answered 2018-07-11 05:37 Rakesh

    You need to pass each element to the function.

    Ex:

    from dateutil.parser import parse
    
    datelist = ["11/Jan/2018 01:01:00", "" ,"01/01/17", "2016-01-02","KK"]
    
    def is_date(string):
        try:
            parse(string)
            return True
        except ValueError:
            return False
    for i in datelist:
        print is_date(i)         #Update 
    

  • answered 2018-07-11 05:42 Sreeram TP

    You are not passing individual dates to the function to check, but the whole list. Which makes the function return False every time.

    You have to edit the code like this to solve the issue

    import dateutil.parser as dparser
    from dateutil.parser import parse
    
    datelist = ["11/Jan/2018 01:01:00", "" ,"01/01/17", "2016-01-02","KK"]
    
    def is_date(string):
        try:
            parse(string)
            return True
        except ValueError:
            return False
    
    with open('output.txt', 'w') as output:
        with open('error_file.txt', 'w') as error:
            for date in datelist:
                if is_date(date) == True:
                    #WRITE TO Output File
                    print(parse(date, fuzzy=True).date())
                else:
                    #WRITE TO Error File
                    print('Error line')
    
    
    # Output
    
    2018-01-11
    Error line
    2017-01-01
    2016-01-02
    Error line