Csv.writerows() Puts Newline After Each Row
Solution 1:
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
withopen('stocks2.csv','w') as f:
to:
withopen('stocks2.csv','wb') as f:
will fix the problem
More info about the issue here:
Solution 2:
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:
withopen('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.
Solution 3:
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:
- csv.writerows() writes the appropriate row of data, and then ends the line with
'\r\n'
- 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].
Solution 4:
I have had the same problem, Om Prakash's fix nearly worked for me, but I am writing a series of strings inside a for loop rather than a list, for me it was only solved as an append instead of w, and an \n instead of '':
withopen('stocks2.csv','a', newline='\n') as f:
f_csv = csv.DictWriter(f, headers)
f_csv.writeheader()
f_csv.writerows(rows)
Solution 5:
May help changing the "lineterminator" parameter as in:
csv.writer(<yourfile>,delimiter="\n",lineterminator="\n")
Post a Comment for "Csv.writerows() Puts Newline After Each Row"