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?
JavaScript
x
14
14
1
import ibm_db
2
import datetime
3
4
sql = ("SELECT CRT_TS FROM TABLE_TIMEST WHERE DATE(CRT_TS) > '2018-01-01' WITH UR;")
5
stmt = ibm_db_exec_immediate(conn,sql)
6
fle = open("c:Testtstamps.txt","w+")
7
dictionary = ibm_db.fetch_both(stmt)
8
while dictionary != False
9
10
fle.write(str(dictionary["CRT_TS"]))
11
dictionary = ibm_db.fetch_both(stmt)
12
13
fle.close()
14
The timestamps on the table are:
JavaScript
1
4
1
2018-06-12 18:50:55.125489
2
2018-06-12 18:57:52.000000
3
2018-06-12 18:58:42.152469
4
I expect to get the same in my output file, instead I get:
JavaScript
1
4
1
2018-06-12 18:50:55.125489
2
2018-06-12 18:57:52
3
2018-06-12 18:58:42.152469
4
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'))