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

Asked

Viewed 822 times

0

I have a row that stores within the file the following columns:

ID   Nome    Telefone   Descrição DataEntrada  HoraEntrada

The ID is basically the line number and it is through it that I will seek to know which line to rewrite. I know that probably to rewrite this I have to read and write the entire 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
  • 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.

  • 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.

  • I want, for example, whenever the line has a component [8], it shows me the amount that was paid for it

  • The "real" suggestion is that you use Sqlite and leave this numbanco and data instead of keeping your data in a text file.

1 answer

3


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.

  • I wish I could send the code, but it’s too big!! But basically he writes each line like this there. I know there are programs that make life easier with databases, but I’m doing it my way precisely to exercise the practice of python, since I’m new. If you want to take a look, I can send you the file to run there.

  • then - take advantage that it is practical, and practice sqlite - possivelmetne you can update your entire program in less d one day, and then you will get a "real application". There’s an e-mail on my profile here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.