Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

csv.writerows() puts newline after each row

This is an example from the O'Reilly Cookbook (truncated dataset)

headers = ['Symbol','Price','Date','Time','Change','Volume']
rows = [{'Symbol': 'AA', 'Volume': 181800, 'Change': -0.18,
         'Time': '9:36am', 'Date': '6/11/2007', 'Price': 39.48},
        {'Symbol': 'AIG', 'Volume': 195500, 'Change': -0.15,
         'Time': '9:36am', 'Date': '6/11/2007', 'Price': 71.38} ]

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

the output file has a \n at the end of each line, and apparently one more at the end. When I bring it into Excel, I get blank lines between each row. The same if I open it with Notepad++.

But, if I more if from a command line, the \n don't show up.

I saw another Q&A about a \n at the end of a file - but this one is about a \n at the end of each line. (And I don't see why more doesn't give the \n.)

I plan to bring the file into OpenOffice Calc.

like image 660
ZZMike Avatar asked Jun 19 '15 03:06

ZZMike


People also ask

What does next () in csv do?

The csv. reader object is an iterator. An iterator is an object with a next() method that will return the next value available or raise StopIteration if no value is available.

Why newline is used in csv?

[1] (1, 2) If newline='' is not specified, newlines embedded inside quoted fields will not be interpreted correctly, and on platforms that use \r\n linendings on write an extra \r will be added. It should always be safe to specify newline='' , since the csv module does its own (universal) newline handling.

How do you not overwrite a CSV file in Python?

Use the append file mode, "a", in the open statement. This is what I mean: open("myfile. csv", "a") The "a" lets you add new rows at the end of the file without overwriting existing rows. Also if the file does net yet exist, then it creates the file for you and lets you write to it.


2 Answers

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

like image 124
paolov Avatar answered Sep 29 '22 08:09

paolov


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.

like image 32
Om Prakash Avatar answered Sep 29 '22 08:09

Om Prakash