0

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.

2
  • Fyi, PEP-8 tells you not to use x == False or x == True but simply x and not x. Unless you are working with an existing codebase that uses the name style you are using I'd also suggest you to read the relevant part of PEP-8. Other python developers who'll work with your code at some point will thnak you for it! Commented Jun 18, 2012 at 0:21
  • I would highly suggest not using an unspecific except statement. This catches ANYTHING that may go wrong and executes your except statements code despite whatever error is thrown. Also, if you want exact traceback to the codeblock, use raise to raise the error that triggered the except statement. Commented Jun 18, 2012 at 4:49

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Currently, if I put a print statement right before the for loop:

if validSyntax == True:
        print Date
        for line in Date:
            ...

it prints out:

1995-06-24

This is not a list and therefore is not iterable.

You are converting it from a list to a date object here:

Date = date(Year, Month, Day)

If you remove this line, it remains as a list rather than of type date.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.