Using a loop to check a date with datetime in python?
Question: I am writing a program that uses a gathers information from a file to output
later on, my problem right now is with testing the information given. I want
to test if the code the user inputted is the same as a date in the file. The
file used contains dates and sales in the form YYYY,MM,DD. I am attempting to
use a for loop to test each line of the file against the user input but I am
getting the error that datetime.date is not iterable. Any
solutions/alternatives? Here is the code,

    
    
    from datetime import date
    
    def DateTest(Date, Position):
    
        firstTry = True
        validSyntax = False
        if validSyntax == False:
    
            if firstTry == True:
                try:
                    Date = Date.strip().split(',')
                    Year = int(Date[0])
                    Month = int(Date[1])
                    Day = int(Date[2])
                    Date = date(Year, Month, Day)
                except:
                    print "That is invalid input."
                    firstTry = False
                else:
                    validSyntax = True
    
            elif firstTry == False:
                Date = raw_input("Please input the desired %s date in the form YYYY,MM,DD: " % Position)
                try :
                    Date = startDate.strip().split(',')
                    Year = int(Date[0])
                    Month = int(Date[1])
                    Day = int(Date[2])
                    Date = date(Year, Month, Day)
                except:
                    print "That is invalid input."
                else:
                    validSyntax = True
                    print" ok got it"
    
            if validSyntax == True:
                for line in Date:
                    line = line.strip().split(',')
                    yearTest = int(line[0])
                    monthTest = int(line[1])
                    dayTest = int(line[2])
                    dateTest = date(yearTest, monthTest, dayTest)
                    if dateTest == Date:
                        "print debug"
    startDate = raw_input("Please input the desired start date: ")
    start = "start"
    Response = DateTest(startDate, start)
    

As you can see I test for valid input and then test for the date being in the
file, which tells me datetime is not iterable.


Answer: Use the strptime it will clean up your code.

    
    
    >>> s = "1994,05,24"
    >>> datetime_obj = datetime.datetime.strptime(s, "%Y,%m,%d")
    >>> datetime_obj
    datetime.datetime(1994, 5, 24, 0, 0)
    

Notice it gives you a datetime object, if you only want the date you can call
.date() on it.

    
    
    >>> date_obj = datetime_obj.date()
    

If you want to loop through a file, you need to have a file object, or a
filename to then convert to a file object. If your file format is csv the csv
module can be helpful, but if the file is just a list of dates, then you may
not need it.

