29

I am trying to write to a csv file via the following

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

fieldnames = ('ItemID', 'Factor', 'FixedAmount')
wr = csv.DictWriter(file, fieldnames=fieldnames)
headers = dict((n, n) for n in fieldnames)

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

However, when I look at the csv-file the first row is empty, the second row is my header, the third row is empty again and the fourth ow shows the entries 1,2 and 3. why does it >skip one row before it makes an entry?

EDIT: using python 3.2

user1266138
  • 615
  • 4
  • 9
  • 14

5 Answers5

55

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()
Lanaru
  • 9,421
  • 7
  • 38
  • 64
  • ran that code in 2.7 and 3.2. In both, changing from 'a' to 'w' eliminated the first empty row, but not the second one. – user1266138 Jul 25 '12 at 15:25
  • I did some further research, and it appears the empty row issue is a bug with python 3.2 and the .csv module. You are supposed to open it in binary mode but that seems to give an error when you call the "writerow" function. – Lanaru Jul 25 '12 at 15:27
  • thanks for looking into this. yes, it gives me the following error: TypeError: 'str' does not support the buffer interface I have a parallel installation of 2.7, though and that does exactly the same – user1266138 Jul 25 '12 at 15:33
  • I found the solution, it is posted in the answer. You had to add another parameter to the constructor. – Lanaru Jul 25 '12 at 15:40
  • Surprised I never ran into this issue before now, but this was a perfect solution! Thanks @Lanaru ! – Blairg23 Apr 11 '16 at 21:32
  • @Lanaru I think binary is the way it worked in Python 2. In Python 3 it was changed to 'w'. – Dr_Zaszuś Feb 26 '18 at 12:12
14

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='')
DSM
  • 342,061
  • 65
  • 592
  • 494
  • get the following error when open as binary: Traceback (most recent call last): File "C:\EclipseWorkspaces\csse120\test\test\__init__.py", line 38, in wr.writeheader() File "C:\Python32\lib\csv.py", line 142, in writeheader self.writerow(header) File "C:\Python32\lib\csv.py", line 153, in writerow return self.writer.writerow(self._dict_to_list(rowdict)) TypeError: 'str' does not support the buffer interface – user1266138 Jul 25 '12 at 15:27
2

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.

0

Could it be that you are opening the output file in append mode?

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

Instead of write mode? If you run the program several times, the output from every run will be in that file...

SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
  • the file does not exist, hence the a. it will also be automatically processed by another application and then deleted – user1266138 Jul 25 '12 at 15:16
-1

I had this problem and it was because I was using \n\r. I changed to \n and it is working now.

Dorian
  • 11
  • 3
  • Can you please clarify what you mean? Where do you use `\n`? – qdread Jan 31 '19 at 20:03
  • I was trying to use sprintf("blah, blah, blah\n\r); to fill my write buffer. This was skipping a line between each occurrence in my .csv file. After changing to sprintf("blah, blah, blah\n); I got the desired output – Dorian Feb 08 '19 at 15:42