使用Python 3.7.3和mysql-连接器8.0.18,我的选择结果如下所示:
[['2019-001', datetime.date(2019, 3, 11), 'Services']]我希望结果是这样的:
[['2019-001', '11/03/2019', 'Services']]代码片段:
...
table = 'Customer'
select = "SELECT * FROM %s"
cursor.execute(select % table)
for row in cursor:
append_list = list(row)
import_list.append(append_list)
print(import_list) #import_list is actually a list of lists
...我无法修改SELECT语句(使用. DATE_FORMAT( DATE,'%d/%m/%Y') ),因为我不知道何时/是否会遇到日期字段,或者当我在MySQL数据库中遍历未知数量的表时,字段的名称。
我所做的调查表明了几种可能性(据我所知):
datetime.date(2011, 1, 3).strftime("%d-%m-%Y") 这似乎不太优雅,但如果这是我必须做的,我需要弄清楚如何做到这一点。
或者还有另一种毕达通的方法来完成我想要完成的事情?
发布于 2021-01-01 07:10:54
您可以使用strftime("%Y/%m/%d")格式化时间戳。例如:
cursor.execute(select % table)
result = cursor.fetchall()
print(result[0][1].strftime("%Y/%m/%d")) # should give you the timestamp in the format you desirehttps://stackoverflow.com/questions/59121155
复制相似问题