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)