0

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.

wawo lala
  • 1
  • 2
  • Please show us what you've tried and what exactly you're struggling with. – Maurice Jan 08 '23 at 13:25
  • 1
    Does this answer your question? [Convert python datetime to epoch with strftime](https://stackoverflow.com/questions/11743019/convert-python-datetime-to-epoch-with-strftime) – Maurice Jan 08 '23 at 13:29

2 Answers2

0
import pandas as pd
pd.to_datetime("2022-12-25 13:38:22.561470", format='%Y-%m-%d %H:%M:%S.%f').timestamp()
Gaurav
  • 59
  • 1
  • 2
0

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
SWEEPY
  • 589
  • 1
  • 6
  • 20