As I wrote in the comment - if you have a text file with data, and will want to keep changing this data, provavelemtne you are using the wrong solution.
Using a "self-contained" database in Python Sqlite format is trivial: it requires no installation and your data is all contained in a single file that you can attach to emails, copy to USB sticks, the same way.
That being said, as you well put in the question, the only way to change data in a text file that does not have fields of fixed width is to rewrite the whole file. The practice is recommended even for files with fixed width fields: in addition to the logical complication of writing at the exact position and length of the data, modern operating systems on current hardware only write on disk multiple 4096 bytes (can be up to 512 on embedded hardware) - that is, there is no performance gain unless the file has multiple megabytes.
Well, you haven’t given any example of your program or how your data is. But the first step, since you will re-write the entire file is ... read the entire file. Then you make changes to the memory of the data you want to change. And then you...write the entire file.
Python CSV has to read each CSV row to 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.
The ideal is to have dates and times as objects datetime.datetime
in Python, and write them in the text file with the method .isoformat
. But to read back these dates, you would have to use the method datetime.strptime
which is for generic dates. So if you want to use the strftime
to put the dates in a more convenient format, fine.
Now, if you are using a database, and declare the column as datetime
, Python does all this for you.
I’m not going to put code in this answer.
I could stay another 10 minutes here and put an example of how to read a CSV as dictionaries, update them and write them back, but two things: it’s a very bad format for you to keep the data, and two: there’s no code for you to give hints or point out how to integrate it. If there were already as you do to read your file in normal use of your program, this would be just one more feature, but not the case.
The suggestion anyway is: take a couple of hours to learn about sqlite - do an interactive tutorial, nice and easy. Then think again about that show.
There are extra columns in the expected result, so you will want to add values in all lines? To edit only one line of the file, you can read about in this question.
– Woss
When I did the program, I did it for products and to have a control, now when I went to improve the program, I want to make it show the day and time that the product left and the amount that was paid by it.
– Thomas Caio
I want, for example, whenever the line has a component [8], it shows me the amount that was paid for it
– Thomas Caio
The "real" suggestion is that you use Sqlite and leave this numbanco and data instead of keeping your data in a text file.
– jsbueno