在python中从SQL查询中选择时间戳
当我得到输出时,如下所示:在python中从SQL查询中选择时间戳,python,mysql,datetime,Python,Mysql,Datetime,当我得到输出时,如下所示: cur.execute('Select ts from testtable where nd_a>%s and nd_b>%s and nd_c>%s',(medA,medB,medC)) result_ts=cur.fetchall() print (result_ts) 当我需要以下格式的输出日期时:YYYY-MM-DD HH:MM:SS 如何将其更改为此格式视图?您可以执行以下操作: cur.execute('Select ts from t
cur.execute('Select ts from testtable where nd_a>%s and nd_b>%s and nd_c>%s',(medA,medB,medC))
result_ts=cur.fetchall()
print (result_ts)
当我需要以下格式的输出日期时:YYYY-MM-DD HH:MM:SS
如何将其更改为此格式视图?您可以执行以下操作:
cur.execute('Select ts from testtable where nd_a>%s and nd_b>%s and nd_c>%s',(medA,medB,medC))
result_ts=cur.fetchall()
print (result_ts)
当前执行“从测试表中选择ts,其中nd_a>%s和nd_b>%s和nd_c>%s”,medA、medB、medC
结果\u ts=cur.fetchall
对于结果中的结果,请执行以下操作:
打印结果[0]
这将打印:
((datetime.datetime(2020, 1, 1, 1, 15, 24),), (datetime.datetime(2020, 1, 1, 1, 15, 38),), (datetime.datetime(2020, 1, 1, 1, 16, 30),), (datetime.datetime(2020, 1, 1, 1, 16, 37),), (datetime.datetime(2020, 1, 1, 1, 17, 8),), (datetime.datetime(2020, 1, 1, 1, 17, 14),))
如果您想要字符串时间戳,那么应该使用该方法
这将为您提供一个转换为时间戳字符串的结果列表:您需要打印值:打印结果[0]%Y-%m-%d%H:%m:%s是默认格式。使用strftime是没有意义的!取决于他们想用它做什么-如果他们只想打印,打印是好的,但如果他们希望用它做点什么,strftime可能会有帮助:
cur.execute('Select ts from testtable where nd_a>%s and nd_b>%s and nd_c>%s',(medA,medB,medC))
result_ts=cur.fetchall()
timestamps = []
for r in results_ts:
timestamps.append(r[0].strftime('%Y-%m-%d %H:%M:%s'))