Does Python csv writer always use DOS end-of-line characters?

Vendetta picture Vendetta · Mar 23, 2012 · Viewed 23k times · Source

I realize that the csv library in Python always generates DOS end-of-line characters. Even if I use the 'wb' mode, even if I use Linux.

import csv

f = open('output.txt', 'wb');
writer = csv.writer(f)
writer.writerow([2,3,4]);
f.close()

The above code always uses '\r\n' as the end of line separator. How can I make it use use '\n' only?

Answer

Niklas B. picture Niklas B. · Mar 23, 2012

You can give your writer instance a custom lineterminator argument in the constructor:

writer = csv.writer(f, lineterminator="\n")