Skip to content
Advertisement

How to write timestamp to txt file when milliseconds are zeroes in python

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

The timestamps on the table are:

JavaScript

I expect to get the same in my output file, instead I get:

JavaScript

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'))

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement