csv writer expected byte like and space between rows
<p>I'm trying to combine several CSV files into one. But I'm getting a space between each row.</p>

<p>I read somewhere I had to change w to wb in <code>filewriter = csv.writer(open("output.csv", "w"))</code></p>

<p>but this gives me the following error:</p>

<pre class="lang-none prettyprint-override"><code>Traceback (most recent call last):
  File "C:\Users\Rasmus\workspace\Portfolio\src\Companies.py", line 33, in &lt;module&gt;
    collect()
  File "C:\Users\Rasmus\workspace\Portfolio\src\Companies.py", line 31, in collect
    Write.UpdateCSV(lst)
  File "C:\Users\Rasmus\workspace\Portfolio\src\Write.py", line 11, in __init__
    filewriter.writerows(lst)
TypeError: a bytes-like object is required, not 'str'
</code></pre>

<p>I'm not sure what to make of this, as the only solution I could find gave me another error.</p>

<p>Are there any other way to get rid of the space between rows?</p>

<p>My code:</p>

<pre><code>import csv
import Write

paths = [
         'Finance.CSV',
         'Energy.CSV',
         'Basic Industries Companies.CSV',
         'Consumer Durables Companies.CSV',
         'Consumer Non-Durables Companies.CSV',
         'Consumer Services Companies.CSV',
         'Health Care Companies.CSV',
         'Public Utilities Companies.CSV',
         'Technology Companies.CSV',
         'Transportation Companies.CSV'
         ]

def collect():
    lst = []

    for path in paths:
        file=open( 'C:/Users/Rasmus/Desktop/Sectors/' + path, "r")
        reader = csv.reader(file)

        for line in reader:
            tmpLst = []
            tmpLst.append(line[0])
            tmpLst.append(line[7])
            lst.append(tmpLst)
            #print(line[0] + ", " + line[7])

    Write.UpdateCSV(lst)

collect()

import csv

class UpdateCSV():
    def __init__(self, lst):
        #print(lst)
        #resultFile = open("output.csv",'w')
        #wr = csv.writer(resultFile, dialect='excel')

        filewriter = csv.writer(open("output.csv", "wb"))

        filewriter.writerows(lst)
</code></pre>

<p>Your method works in Python 2.</p>

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

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

<pre><code>with open("output.csv", "w", newline='') as f:
    filewriter = csv.writer(f)
</code></pre>

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

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

<pre><code>with open("output.csv","w") as f:
    filewriter = csv.writer(f,lineterminator="\n")
</code></pre>

<p>I had asked a question some time ago and just updated my answer to reflect that: <a href="http://stackoverflow.com/questions/38808284/portable-way-to-write-csv-file-in-python-2-or-python-3">portable way to write csv file in python 2 or python 3</a></p>

<p>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...)</p>

python python-3.x csv