Skip to content Skip to sidebar Skip to footer

Csv Writer Expected Byte Like And Space Between Rows

I'm trying to combine several CSV files into one. But I'm getting a space between each row. I read somewhere I had to change w to wb in filewriter = csv.writer(open('output.csv', '

Solution 1:

Your method works in Python 2.

But in python 3 you cannot open a text file in binary mode or the write method will expect bytes.

A correct way of doing it in python 3 (with, added the with statement that ensures that the file is closed when exiting the with block):

withopen("output.csv", "w", newline='') as f:
    filewriter = csv.writer(f)

(omitting the newline parameter works but inserts a "blank" line in windows systems because of extra carriage return, that's probably the problem you're describing)

EDIT: I checked csv module documentation, and there's an even better way to do this, works with python 2 and 3, which is to change csv lineterminator:

withopen("output.csv","w") as f:
    filewriter = csv.writer(f,lineterminator="\n")

I had asked a question some time ago and just updated my answer to reflect that: portable way to write csv file in python 2 or python 3

EDIT 2: 2.7.12 and 3.5.2 version of python don't need all this. Seems that someone fixed the problem for good. But not everyone can upgrade even the minor versions of python (because of company policies, qualified tools depending on a given version...)

Post a Comment for "Csv Writer Expected Byte Like And Space Between Rows"