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

1
  • Also see the accepted answer to Inserting Line at Specified Position of a Text File in Python. 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. Commented Aug 12, 2014 at 5:48

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.