Loop to replace integer date into actual date and create new text file

0
Hello, I am a Python beginner.
I am working with a txt file in Python. The file contains a time series for a given variable. Currently, my time variable is reported in the form of a range (1:100). I would like to replace such integers with the actual dates.

What I would need to do is via a loop as follows:

  1. Read the initial .txt file
  2. Replace the date range (1:100) in the ‘time’ column with the actual dates (01-01-2010 - …) and save the output as a new .txt file.

So far, I have tried this:

with open('F://tg.txt','r') as infile:
      with open('F://tg2.txt','w') as outfile:
         rdr = csv.reader(infile, delimiter=' ')
         wtr = csv.writer(outfile, delimiter=' ')
         header = next(rdr)
         wtr.writerow(header)
         for row in rdr:   
         pd.to_datetime(range(1, 100), origin="1950-01-01", unit="D", utc=True)
         wtr.writerow(row) 

The loop manages to create both an input and ouput file. However, the code I am using for ‘datetime’ does not change the integers to the actual date I want. Is there a way to solve this issue in Python? Thanks!

The line:

pd.to_datetime(range(1, 100), origin="1950-01-01", unit="D", utc=True)

is converting the fixed range (from 1 up to but excluding 100) to a time series whose origin is 1950-01-01 and is returning the result.

You’re not giving it any data from the row, nor are you changing the contents of the row. It’s just: read row, make and discard time series (unrelated to row), write row.