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()
4
  • 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 ] Commented Nov 3, 2014 at 20:46
  • Correct me if I am wrong but that backslash is the continuation character, isnt it not? Commented Nov 13, 2014 at 10:24
  • 1
    This is old, but I noticed no one responded. The continuation character is not needed if you have a comma. Commented Oct 13, 2015 at 21:46
  • See also: How do I read and write CSV files with Python? Commented Jan 17, 2017 at 4:44

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

ERROR: TypeError: 'newline' is an invalid keyword argument for this function ?
@SaadBinShahid: then you're not using Python 3.
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.

16 Comments

is this caused by Windows automatically adding nl-characters when writing text instead of binary?
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 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: it does matter which version of Python the OP is using, because of the 2/3 str/bytes changes.
|
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

6 Comments

thank, but 'wb' gives me an error.
is this the same error you get? stackoverflow.com/questions/5471158/…
does the solution help?
NO it didn't...I get this error
copy what i have up directly, this works for me
|
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').

1 Comment

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!!
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')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.