'How to insert a letter in a certain space in txt file using python?

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



Solution 1:[1]

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
    

Solution 2:[2]

Something like this:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 The Myth
Solution 2 stefan_aus_hannover