csv.writerows() puts newline after each row

It's an extra carriage return, and this is a Windows-specific issue not related to Python 2/3 differences. If you open up your file in Notepad++ and enable Show all characters, you'll see the following:

Symbol,Price,Date,Time,Change,Volume[CR]
[CR][LF]
AA,39.48,6/11/2007,9:36am,-0.18,181800[CR]
[CR][LF]
AIG,71.38,6/11/2007,9:36am,-0.15,195500[CR]
[CR][LF]

This is because Python on Windows is translating your line ending from '\n' to '\r\n', while the writerows() function is already adding '\r\n' to the end of every line. What's happening:

  1. csv.writerows() writes the appropriate row of data, and then ends the line with '\r\n'
  2. Python's internal handling (because you're on Windows) sees the end of line '\n' and thinks it needs to change that to '\r\n'. So you get the '\r\r\n'.

The reason you don't see printing to the console have issues is because it's not detecting the extra '\r' as a new line, where as Excel and Notepad++ are.

For Python 3, you should be using the newline='' option as documented here: https://docs.python.org/3/library/csv.html.

csv.writer(csvfile, dialect='excel', **fmtparams)

Return a writer object responsible for converting the user’s data into delimited strings on the given file-like object. csvfile can be any object with a write() method. If csvfile is a file object, it should be opened with newline='' [1].


I came across this issue on windows for Python 3. I tried changing newline parameter while opening file and it worked properly with newline=''.

Add newline='' to open() method as follows:

with open('stocks2.csv','w', newline='') as f:
    f_csv = csv.DictWriter(f, headers)
    f_csv.writeheader()
    f_csv.writerows(rows)

It will work as charm.

Hope it helps.


This problem occurs only with Python on Windows.

In Python v3, you need to add newline='' in the open call per:

Python 3.3 CSV.Writer writes extra blank rows

On Python v2, you need to open the file as binary with "b" in your open() call before passing to csv

Changing the line

with open('stocks2.csv','w') as f:

to:

with open('stocks2.csv','wb') as f:

will fix the problem

More info about the issue here:

CSV in Python adding an extra carriage return, on Windows