As I wrote in the comment - if you have a text file with data, and you want to keep changing this data, you probably are using the wrong solution.
Using a "self-contained" database in SQLite format in Python is trivial: it does not require any installation and your data is all contained in a single file that you can attach to e-mails, copy to pendrives in the same way.
That said, as you well put in the question, the only way to change data in a text file that does not have fixed width fields and rewrite the whole file. The practice is even recommended for files with fixed-width fields: in addition to the logical complication of typing in the exact position and length of the data, modern operating systems in current hardare only write to disk multiples of 4096 bytes (it can be up to 512 in embedded hardware) - that is, there is no performance gain unless the file has multiple megabytes.
Well, you did not give any examples of your program or how your data is. But the first step, since you're going to re-write the whole file is ... read the whole file. Hence you make changes in memory, of the data you want to change. And then you ... save the entire file.
The Python CSV can read each line of the CSV for a dictionary, where the column header is the key, and the value, each column.
To change the file, you process each dictionary and set the date and time.
Ideally, you should have the dates and times of datetime.datetime
objects in Python, and write them in the text file with the .isoformat
method. But to read back those dates, you would have to use the datetime.strptime
method that is for generic dates. So if you want to use strftime
to put dates in a more convenient format, fine.
Now, if you're using a database, and declaring the column as datetime
, Python does it all for you.
I'm not going to code this post.
I could stay another 10 minutes here and post an example of how to read a CSV like dictionaries, update them and write them back, but two things: it's a very bad format for you to keep the data, and two: it has none code to give tips or point out how to integrate this. If you had already there how you do to read your file in normal use of your program, this would only be one more feature, but it is not the case.
The suggestion anyway is: take a couple of hours to learn about sqlite - do an iterative tyutorial, calmly. Then think about this program again.