Python CSV How to rewrite only one line of the file?

0

I have a line that stores the following columns inside the file:

ID   Nome    Telefone   Descrição DataEntrada  HoraEntrada

The ID is basically the line number and it's through it that I'm going to search what line to rewrite. I know that probably to rewrite this I have to read and write the whole file, however, I do not know how to add other values to this line.

Desired result:

ID   Nome    Telefone   Descrição    DataEntrada    HoraEntrada    DataSaída HoraSaída    Valor
    
asked by anonymous 28.11.2017 / 14:45

1 answer

2

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.

    
29.11.2017 / 13:44