Home > Net >  How to insert a letter in a certain space in txt file using python?
How to insert a letter in a certain space in txt file using python?

Time:03-15

I want to put letter T in between dd-mm-yyyy hh:mm:ss.ms as dd-mm-yyyyThh:mm:ss.ms, specifically 07-05-2007 06:00:01.500 => 07-05-2007T06:00:01.500. How to use python to achieve this. Here is my data in txt format. My data as txt file

CodePudding user response:

This is a very wrong practice. However, in this scenario you can always use .replace(' ','T') but I still suggest using datetime module.

Here's a small look of how it would look like:

import datetime as dt
for time in df['EPOCH']:
    your_time = time.strftime("%d-%m-%YT%H:%M:%S.500") #.500 because looks like all your ms is 500

Here, this code would work if your time in the column 'ELOF' is a datetime module which it should be. And if it is not, you can simply convert it to a datetime object using:

import datetime as dt
for time in df['EPOCH']:
    datetime_object = dt.datetime.strptime(time, "%d-%m-%YT%H:%M:%S.500") 
    #THIS IS THE SAME AS ABOVE JUST IF YOUR TIME IS NOT A DT OBJECT
    #Now you can do whatever you want with the datetime_object
    

CodePudding user response:

Something like this:

df['EPOCH'] = df['EPOCH'].str.replace(' ','T')
  • Related