0

I'm having the same issue reported here, but the solutions aren't really related to the extra lines, they're pointing out that the original file is being opened incorrectly.

Python 3.3 CSV.Writer writes extra blank rows

I have the same issue writing out a list, the following code

sample_data = [[1,2],[3,4],[5,6]]
with open('text.csv', 'w') as csvfile:                
    writer = csv.writer(csvfile, dialect=csv.excel)
    writer.writerows(sample_data)

produces

1,2

3,4

5,6

Whilst changing the dialect to dialect=csv.unix_dialect (which as far as I can see only changes from lineterminator = '\r\n' to lineterminator = '\n'

produces the correct result

1,2
3,4
5,6

Am I doing something wrong, quite likely given I've just migrated to Py3.3

Community
  • 1
  • 1
David Waterworth
  • 2,214
  • 1
  • 21
  • 41
  • The last list should be split over 3 lines with no gaps. Sorry I cannot figure out how to do this on the forum – David Waterworth Dec 11 '13 at 04:37
  • What platform is this? Linux, Mac, Windows? – David K. Hess Dec 11 '13 at 04:43
  • 3
    ? As mentioned in the linked question, in Python 3 you need to open csv files for writing using `open("text.csv", "w", newline="")`. Are you saying you still get the extra lines when you use the correct parameters? – DSM Dec 11 '13 at 04:46
  • Hi DSM, I didn't notice the newline="" parameter, I was focused on the "w". using newline="" works thanks for both csv.unix_dialect and csv.excel (because presumably without newline="" the file object adds \r and then the csv writer adds with \n (unix_dialect) or \r\n (excel dialect) – David Waterworth Dec 11 '13 at 22:33

0 Answers0