76

I am trying to write to CSV file but there are blank rows in between. How can I remove the blank rows?

import csv
b = open('test.csv', 'w')
a = csv.writer(b)
data = [['Me', 'You'],\
        ['293', '219'],\
        ['54', '13']]
a.writerows(data)
b.close()
davidism
  • 98,508
  • 22
  • 317
  • 288
user2031063
  • 937
  • 1
  • 7
  • 10
  • 1
    You don't need backslashes at the end of the lines defining `data`. Python ignores newlines inside the enclosing square brackets, i.e. between `[` and `]` – Zags Nov 03 '14 at 20:46
  • Correct me if I am wrong but that backslash is the continuation character, isnt it not? – KennethG Nov 13 '14 at 10:24
  • 1
    This is old, but I noticed no one responded. The continuation character is not needed if you have a comma. – gprx100 Oct 13 '15 at 21:46
  • See also: [How do I read and write CSV files with Python?](http://stackoverflow.com/q/41585078/562769) – Martin Thoma Jan 17 '17 at 04:44

5 Answers5

117

The way you use the csv module changed in Python 3 in several respects (docs), at least with respect to how you need to open the file. Anyway, something like

import csv
with open('test.csv', 'w', newline='') as fp:
    a = csv.writer(fp, delimiter=',')
    data = [['Me', 'You'],
            ['293', '219'],
            ['54', '13']]
    a.writerows(data)

should work.

DSM
  • 291,791
  • 56
  • 521
  • 443
21

If you're using Python 2.x on Windows you need to change your line open('test.csv', 'w') to open('test.csv', 'wb'). That is you should open the file as a binary file.

However, as stated by others, the file interface has changed in Python 3.x.

Wilduck
  • 12,774
  • 9
  • 54
  • 84
  • 1
    is this caused by Windows automatically adding nl-characters when writing text instead of binary? – Gjordis Feb 04 '13 at 19:07
  • I tried by I get this error: w.writrows(data) 'str' does not support the buffer interface – user2031063 Feb 04 '13 at 19:08
  • I am using python33....don't know if that makes any difference! – user2031063 Feb 04 '13 at 19:09
  • @user2031063 Version of python shouldn't matter. Your error message suggests that you've simply mixed up your `w` and `a` objects. Are you sure you have all your names right? – Wilduck Feb 04 '13 at 19:12
  • @Gjordis Exactly. For more detail, there's a link to the docs in my answer. – Wilduck Feb 04 '13 at 19:12
  • Yes, I am sure. Help please – user2031063 Feb 04 '13 at 19:13
  • @user2031063 are you _sure_? Why does your error message say that the line `w.writerows(data)` is causing an error then? That line isn't in your example code. You do have a line `a.writerows(data)`. Your example code _should_ work as written. – Wilduck Feb 04 '13 at 19:14
  • Can you kindly run my code with 'wb' and see if it works. – user2031063 Feb 04 '13 at 19:15
  • Both @Aditya and I have confirmed that the code in your example (with `'w'` changed to `'wb'`) works correctly. You are definitely running something different than what you've written above. – Wilduck Feb 04 '13 at 19:17
  • sorry it is a.writerows(data) and not w.writerows(data). my code work as is and gives me blank lines..when I try 'wb' gives me the error message. – user2031063 Feb 04 '13 at 19:18
  • It is not running for me – user2031063 Feb 04 '13 at 19:19
  • this is the error I get >>> Traceback (most recent call last): File "C:/Python33/CSV-test.py", line 7, in a.writerows(data) TypeError: 'str' does not support the buffer interface >>> – user2031063 Feb 04 '13 at 19:20
  • 2
    @Wilduck: it *does* matter which version of Python the OP is using, because of the 2/3 str/bytes changes. – DSM Feb 04 '13 at 19:21
  • @DSM Yep, I'm just realizing that. Definitely got mixed up by the docs. I'll try to clarify my answer in an edit. – Wilduck Feb 04 '13 at 19:23
  • Definitely accept DSM's answer. He knows what he's doing. @DSM Yep, I'm a little behind the times, both for this question and for my python version apparently. I learned something new today =). – Wilduck Feb 04 '13 at 19:27
  • @DSM...guide me please – user2031063 Feb 04 '13 at 19:30
15
import csv

hello = [['Me','You'],['293', '219'],['13','15']]
length = len(hello[0])

with open('test1.csv', 'wb') as testfile:
    csv_writer = csv.writer(testfile)
    for y in range(length):
        csv_writer.writerow([x[y] for x in hello])

will produce an output like this

Me You
293 219
13 15

Hope this helps

sbru
  • 837
  • 4
  • 19
  • 36
John Smith
  • 1,019
  • 2
  • 12
  • 34
5

You need to open the file in binary b mode to take care of blank lines in Python 2. This isn't required in Python 3.

So, change open('test.csv', 'w') to open('test.csv', 'wb').

Aditya
  • 369
  • 6
  • 20
  • I tried by I get this error: w.writrows(data) 'str' does not support the buffer interface. I am using python33..don't know if that makes any difference!! – user2031063 Feb 04 '13 at 19:10
3

Pyexcel works great with both Python2 and Python3 without troubles.

Fast installation with pip:

pip install pyexcel

After that, only 3 lines of code and the job is done:

import pyexcel
data = [['Me', 'You'], ['293', '219'], ['54', '13']]
pyexcel.save_as(array = data, dest_file_name = 'csv_file_name.csv')
Danny Lessio
  • 528
  • 4
  • 12