0

I'm trying to delete a row and update the changes in the CSV file. I used the following code to write the changes into the file. Each time I run this code, it inserts empty rows alternatively.

with open(self.__fileName, 'w') as writeFile:
    writer = csv.writer(writeFile)
    writer.writerows(file)

Here variable file is the updated data after deleting the row. The delete function works fine. I checked it with a print statement.

If I delete Row 2, my file should look like the following:

enter image description here

But I delete and run this code, my looks like the following:

enter image description here

steveb
  • 5,382
  • 2
  • 27
  • 36
merry
  • 15
  • 4
  • 1
    Please do not share information as images unless absolutely necessary. See: https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors. We're going to need a [mcve]. Also, variable and function names should generally follow the `lower_case_with_underscores` style. – AMC Feb 16 '20 at 07:13
  • Could you please share the contents of your `file` object?? – Shubham Sharma Feb 16 '20 at 07:15
  • I suspect the issue is that each line in `file` contains a newline and `writerows` likely automatically appends a newline, so you end up with an extra newline for each line. – steveb Feb 16 '20 at 07:17
  • @ShubhamSharma [['Row 1', 'x1', 'y1'], ['Row 3', 'x3', 'y3'], ['Row 4', 'x4', 'y4']] – merry Feb 16 '20 at 07:23
  • @steveb the `file` doesn't contain any empty list. So I doubt it contains a newline – merry Feb 16 '20 at 07:25
  • @mariam I wasn't suggesting empty line lists, I was suggesting a line may have a trailing `\n` (or something). As in `abc, def\n`. However, it looks like you got your questions answered. – steveb Feb 16 '20 at 07:33

0 Answers0