I’m extracting create timestamp from DB/2 table and writing it to a txt file to be used in later processing. Python writes the timestamp correctly except when the milliseconds are zeroes. When the milliseconds are zeroes, it does not write the milliseconds. How can I make it write the zero milliseconds?
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:Testtstamps.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()
The timestamps on the table are:
2018-06-12 18:50:55.125489 2018-06-12 18:57:52.000000 2018-06-12 18:58:42.152469
I expect to get the same in my output file, instead I get:
2018-06-12 18:50:55.125489 2018-06-12 18:57:52 2018-06-12 18:58:42.152469
How do I get python to write the zeroes in the milliseconds?
Advertisement
Answer
Your timestamps seem to already be datetime objects, so you just use their own strftime method:
fle.write(dictionary["CRT_TS"].strftime('%Y-%m-%d %H:%M:%S.%f'))