0

just found out how to write and read to a csv file. It's very helpful, but im having some trouble with it. I noticed that when I scroll down in the csv file, it creates new lines, which then messes up when I want to append data to the csv file (it shows up at the bottom of a bunch of empty lines). So what I plan on doing is: Going down the document until it finds an empty row and get that line number with the line_num reader object. So then I would just need to be able to write to a specific line. Is that possible?

I have been using this webpage as a guide. https://docs.python.org/3/library/csv.html

Kyle Me
  • 336
  • 2
  • 5
  • 13
  • Also see the accepted answer to [Inserting Line at Specified Position of a Text File in Python](http://stackoverflow.com/a/1325927/908494). Of course you can't directly wrap the `csv` module around the `fileinput` module, but your idea of getting the line number and then *doing something*, the `fileinput` stuff is that `something`; you'll just need to use `csv` to write a row to an `io.BytesIO` or `io.StringIO` instead of to the real file, then use `fileinput` to write its `getvalue()` as a line. – abarnert Aug 12 '14 at 05:48

1 Answers1

1

Can't be done. If you want to write to the middle of the file, you need to read the whole thing in and write the whole thing back out with your change.

Here's a thread about the same thing.

Community
  • 1
  • 1
Patrick Collins
  • 10,306
  • 5
  • 30
  • 69