MCO
MCO

Reputation: 314

ValueError: time data '(datetime.datetime(2018, 8, 21, 14, 14, 18, 233000),)' does not match format '%Y-%m-%d'

I'm trying to program a script that pulls data from SQL and push it into Quickbase. I only need Year, Month, and Day from my rows

here's the output from print result

(datetime.datetime(2018, 11, 30, 19, 52, 32, 927000),)
(None,)
(datetime.datetime(2018, 10, 17, 21, 39, 37, 163000),)
(datetime.datetime(2016, 12, 30, 20, 14, 23, 133000),)
(datetime.datetime(2018, 10, 17, 21, 31, 21, 853000),)
(datetime.datetime(2017, 2, 27, 21, 26, 51, 307000),)
(datetime.datetime(2018, 12, 20, 20, 35, 29, 997000),)
(datetime.datetime(2019, 9, 5, 15, 29, 22, 967000),)
(datetime.datetime(2018, 8, 13, 21, 57, 3, 307000),)
(datetime.datetime(2018, 10, 17, 21, 28, 23, 60000),)

Here is what I have

for result in cursor.fetchall():
    print(result)
    result = datetime.strptime(str(result), '%Y-%m-%d')



    #client.edit_record(rid = result , fields = {'99' : '1' }, database=CONTACTS_TABLE)
cursor.commit()
cursor.close()

heres my error

ValueError: time data '(datetime.datetime(2018, 8, 21, 14, 14, 18, 233000),)' does not match format '%Y-%m-%d'

Any input appreciated!

Upvotes: 1

Views: 70

Answers (2)

Emmanuel Arias
Emmanuel Arias

Reputation: 484

you have some troubles trying to manage that data structure.

You don't need to cast to string, so this part is bad:

datetime.strptime(str(result), '%Y-%m-%d')

You should access to the first element of the tuples and then use strftime

>>> dd = (datetime.datetime(2018, 11, 30, 19, 52, 32, 927000),)
>>> datetime.datetime.strftime(dd[0], '%Y-%m-%d')
'2018-11-30'

Upvotes: 0

Emmanuel Arias
Emmanuel Arias

Reputation: 484

I think that you have the problem that you are trying to convert a tuple to str, that is not the problem but that don't help you.

I recommend do something like this if you want to save a str:

for result in cursor.fetchall():
    print(result)
    result = '%d-%d-%d' % (result[0].year, result[0].month, result[0].day)

or if you want to save a date object, just create a new datetime.date with that values

Upvotes: 1

Related Questions