Search code examples
pythoncsvline-endingswriter

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


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?


Solution

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

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