csv.write skipping lines when writing to csv

Solution is to specify the "lineterminator" parameter in the constructor:

file = open('P:\test.csv', 'w')

fields = ('ItemID', 'Factor', 'FixedAmount')
wr = csv.DictWriter(file, fieldnames=fields, lineterminator = '\n')

wr.writeheader()
wr.writerow({'ItemID':1, 'Factor': 2, 'FixedAmount':3})
file.close()

Twenty quatloos says you're running under windows (well, a hundred, given that your file is called P:\test.csv). You're probably getting extra \rs.

[edit]

Okay, since using binary mode causes other problems, how about this:

file = open('P:\test.csv', 'w', newline='')

You need to pass an additional parameter to the 'open()' function:

file = open('P:\test.csv', 'a', newline='')

This will keep it from skipping lines.

Tags:

Python

Csv