I am trying to convert multiple .xls files into one .csv file so i can use in a later script.
I found most of this code in other Stackoverflow pages but when trying to run the code below i get the following error:
Error:
Traceback (most recent call last):
File "script_base_teste.py", line 13, in <module>
wr.writerow(sh.row_values(rownum))
TypeError: a bytes-like object is required, not 'str'
Code:
import xlrd
import csv
for num in range(1,4):
wb = xlrd.open_workbook("file"+str(num)+".xls")
sh = wb.sheet_by_name("Sheet 1")
your_csv_file = open("file"+str(num)+".csv", "wb")
wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)
for rownum in range(0,sh.nrows):
wr.writerow(sh.row_values(rownum))
your_csv_file.close()
fout=open("result.csv","a")
first file:
for line in open("file1.csv"):
fout.write(line)
now the rest:
for num in range(2,3):
f = open("file"+str(num)+".csv")
f.next() # skip the header
for line in f:
fout.write(line)
f.close()
fout.close()
I'm using Python3 and I got most of the code from this and this