我正在从 DB/2 表中提取创建时间戳并将其写入一个 txt 文件以供以后处理使用。 Python 正确写入时间戳,除非毫秒为零。当毫秒为零时,它不会写入毫秒。我怎样才能让它写入零毫秒?
import ibm_db
import datetime
sql = ("SELECT CRT_TS FROM TABLE_TIMEST WHERE DATE(CRT_TS) > '2018-01-01' WITH UR;")
stmt = ibm_db_exec_immediate(conn,sql)
fle = open("c:\Test\tstamps.txt","w+")
dictionary = ibm_db.fetch_both(stmt)
while dictionary != False
fle.write(str(dictionary["CRT_TS"]))
dictionary = ibm_db.fetch_both(stmt)
fle.close()
表格上的时间戳是:
2018-06-12 18:50:55.125489
2018-06-12 18:57:52.000000
2018-06-12 18:58:42.152469
我希望在我的输出文件中得到相同的结果,但我得到的是:
2018-06-12 18:50:55.125489
2018-06-12 18:57:52
2018-06-12 18:58:42.152469
如何让 python 在毫秒内写入零?
最佳答案
您的时间戳似乎已经是 datetime 对象,因此您只需使用它们自己的 strftime 方法:
fle.write(dictionary["CRT_TS"].strftime('%Y-%m-%d %H:%M:%S.%f'))
关于python - python中毫秒为零时如何将时间戳写入txt文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57603155/