Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does CSV file contain a blank line in between each data line when outputting with Dictwriter in Python [duplicate]

I am using DictWriter to output data in a dictionary to a csv file. Why does the CSV file have a blank line in between each data line? It's not a huge deal, but my dataset is big and doesn't fit into one csv file because it has too many lines since the "double-spacing" doubles the number of lines in the file.

My code for writing to the dictionary is:

headers=['id', 'year', 'activity', 'lineitem', 'datum'] output = csv.DictWriter(open('file3.csv','w'), delimiter=',', fieldnames=headers) output.writerow(dict((fn,fn) for fn in headers)) for row in rows:     output.writerow(row) 
like image 967
myname Avatar asked Jan 05 '12 17:01

myname


People also ask

Why CSV writer adds blank rows?

The way Python handles newlines on Windows can result in blank lines appearing between rows when using csv. writer . In Python 2, opening the file in binary mode disables universal newlines and the data is written properly.

Can CSV files have empty lines?

I just checked: Python's CSV parser ignores empty lines. I guess that's reasonable. Yes, I agree an empty line within a quoted field means a literal empty line.

What is stored in each line of a CSV file?

A comma-separated values (CSV) file is a delimited text file that uses a comma to separate values. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format.


2 Answers

By default, the classes in the csv module use Windows-style line terminators (\r\n) rather than Unix-style (\n). Could this be what’s causing the apparent double line breaks?

If so, you can override it in the DictWriter constructor:

output = csv.DictWriter(open('file3.csv','w'), delimiter=',', lineterminator='\n', fieldnames=headers) 
like image 137
dhwthompson Avatar answered Sep 20 '22 19:09

dhwthompson


From http://docs.python.org/library/csv.html#csv.writer:

If csvfile is a file object, it must be opened with the ‘b’ flag on platforms where that makes a difference.

In other words, when opening the file you pass 'wb' as opposed to 'w'.
You can also use a with statement to close the file when you're done writing to it.
Tested example below:

from __future__ import with_statement # not necessary in newer versions import csv headers=['id', 'year', 'activity', 'lineitem', 'datum'] with open('file3.csv','wb') as fou: # note: 'wb' instead of 'w'     output = csv.DictWriter(fou,delimiter=',',fieldnames=headers)     output.writerow(dict((fn,fn) for fn in headers))     output.writerows(rows) 
like image 40
mechanical_meat Avatar answered Sep 23 '22 19:09

mechanical_meat