0

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", "w"))

but this gives me the following error:

Traceback (most recent call last):
  File "C:\Users\Rasmus\workspace\Portfolio\src\Companies.py", line 33, in <module>
    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'

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

Are there any other way to get rid of the space between rows?

My 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)
martineau
  • 119,623
  • 25
  • 170
  • 301
FareJump
  • 113
  • 2
  • 10

1 Answers1

2

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):

with open("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:

with open("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...)

Community
  • 1
  • 1
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219