Hi I have packet time in the format 2022-12-25 13:38:22.561470
and I want to change this to this format 1671971890.8821542
using python but I don't know how to do it. 2022-12-25 13:38:22.561470
format is store in text file.
CodePudding user response:
import pandas as pd
pd.to_datetime("2022-12-25 13:38:22.561470", format='%Y-%m-%d %H:%M:%S.%f').timestamp()
CodePudding user response:
You can use datetime
module to convert your string to datetime object and then get your timestamp :
from datetime import datetime
with open("packet.txt", "r") as f:
packet_time = f.readline()
print(datetime.strptime(packet_time, "%Y-%m-%d %H:%M:%S.%f").timestamp())
Text file content :
2022-12-25 13:38:22.561470
Output :
1671971902.56147