Python ValueError:时间数据';(datetime.datetime(2018,8,21,14,14,18233000)和#x27;与格式不匹配';%Y-%m-%d';

Python ValueError:时间数据';(datetime.datetime(2018,8,21,14,14,18233000)和#x27;与格式不匹配';%Y-%m-%d';,python,datetime,format,valueerror,Python,Datetime,Format,Valueerror,我试图编写一个脚本,从SQL中提取数据并将其推送到Quickbase中。我只需要一年,一个月,一天从我的行 这是打印结果的输出 (datetime.datetime(2018,11,30,19,52,32927000) (无) (datetime.datetime(2018,10,17,21,39,37,163000) (datetime.datetime(2016,12,30,20,14,23133000),) (datetime.datetime(2018,10,17,21,31,21,85

我试图编写一个脚本,从SQL中提取数据并将其推送到Quickbase中。我只需要一年,一个月,一天从我的行

这是打印结果的输出

(datetime.datetime(2018,11,30,19,52,32927000)
(无)
(datetime.datetime(2018,10,17,21,39,37,163000)
(datetime.datetime(2016,12,30,20,14,23133000),)
(datetime.datetime(2018,10,17,21,31,21,853000),)
(datetime.datetime(2017,2,27,21,26,51307000)
(datetime.datetime(2018,12,20,20,35,29997000),)
(datetime.datetime(2019、9、5、15、29、22、967000)
(datetime.datetime(2018,8,13,21,57,3307000),)
(datetime.datetime(2018、10、17、21、28、23、60000)
这是我的

对于游标中的结果。fetchall(): 打印(结果) 结果=datetime.strtime(str(结果),“%Y-%m-%d”) #client.edit_记录(rid=result,fields={'99':'1'},database=CONTACTS_TABLE) cursor.commit() cursor.close() 这是我的错误

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

欢迎任何意见

我认为您有一个问题,您正在尝试将元组转换为str,这不是问题,但对您没有帮助

如果要保存str,建议执行以下操作:

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

或者,如果要保存日期对象,只需创建一个新的datetime.date,并使用该值

您在尝试管理该数据结构时遇到了一些问题

您不需要强制转换为
字符串
,因此此部分不好:

datetime.strptime(str(result), '%Y-%m-%d')
您应该访问元组的第一个元素,然后使用


感谢它的工作,你能给我一个实例,它处理datetime中的null/None值,以及如何解析/跳过它吗?我将编写类似这样的内容:
if result[0]:result=
>>> dd = (datetime.datetime(2018, 11, 30, 19, 52, 32, 927000),)
>>> datetime.datetime.strftime(dd[0], '%Y-%m-%d')
'2018-11-30'