Jed Christiansen
Jed Christiansen

Reputation: 669

Import string from CSV to DateProperty in App Engine

I'm trying to read a CSV file with a date format "06/01/05" (month/date/year) into a DateProperty (not DateTimeProperty) in App Engine (Python). Here's what I currently have:

for row in mycsvfiledata:
    obj = MyObject()
    datetemp = row[1]
    cohorttemp = datetime.datetime.strptime(datetemp, "%m/%d/%y")
    obj.cohort_date.month = cohorttemp.month
    obj.cohort_date.day = cohorttemp.day
    obj.cohort_date.year = cohorttemp.year

and I get the error: [... omitted ...] obj.cohort_date.month = cohorttemp.month AttributeError: 'NoneType' object has no attribute 'month'

I've also tried:

obj.cohort_date = datetime.date.strptime(datetemp, "%m/%d/%y")

(but strptime only seems to generate a datetime object, not a date object)

obj.cohort_date = datetime.datetime.strptime(datetemp, "%m/%d/%y")

(but "cohort_date" is a DateProperty in App Engine, and I get "BadValueError: Property cohort_date must be a date, not a datetime" when I try that)

It all works fine if I just make "cohort_date" a DateTimeProperty, but I don't want that (for various reasons). I'd appreciate any guidance on this!

Upvotes: 0

Views: 223

Answers (1)

Greg
Greg

Reputation: 10360

datetime instances have a date() method that returns the corresponding date instance, so:

obj.cohort_date = datetime.strptime(datetemp, "%m/%d/%y").date()

Upvotes: 2

Related Questions