我的python代码从mySql服务器数据库中检索datetime列。我可以打印存储在服务器上的日期时间(例如,2018-10-22,但是我需要以一种更具可读性的方式打印它,比如22-10-2018。基本上,row保存日期时间数据,如以下代码所示。谢谢你的帮助。
if mycursor.rowcount > 0:
print("Total rows are: ", len(myresult))
for row in myresult:
print("Record Number: " + str(countLines))
print("-----------------")
print("Time: ", row[0])
print("Latitude: ", row[1])
print("Longitude: ", row[2])
print("Accuracy: ", row[3])
print("Place: ", row[4])
发布于 2021-01-14 09:50:16
如果time
是datetime
对象,则可以使用strftime()
from datetime import datetime
row[0].strftime('%d-%m-%Y')
输出
t = datetime(2018, 10, 22)
t.strftime('%d-%m-%Y')
#'22-10-2018'
如果您的数据是string
,则需要首先将其定义为datetime
对象。
发布于 2021-01-14 10:15:46
您可以更改以下行:
print("Time: ", row[0])
至:
print("Time: ", row[0].strftime('%d-%m-%Y')
https://stackoverflow.com/questions/65712246
复制相似问题