I was running into encoding errors so I used the Unicode CSV reader found here https://docs.python.org/2/library/csv.html. This answer is for Python 2. If you need a Python 3 answer let me know.
import csv, codecs, cStringIO, glob, os
import xlsxwriter
class UTF8Recoder:
"""Iterator that reads an encoded stream and reencodes the input to UTF-8"""
def __init__(self, f, encoding):
self.reader = codecs.getreader(encoding)(f)
def __iter__(self):
return self
def next(self):
return self.reader.next().encode("utf-8")
class UnicodeReader:
"""A CSV reader which will iterate over lines in the CSV file "f",
which is encoded in the given encoding."""
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
f = UTF8Recoder(f, encoding)
self.reader = csv.reader(f, dialect=dialect, **kwds)
def next(self):
row = self.reader.next()
return [unicode(s, "utf-8") for s in row]
def __iter__(self):
return self
wb = xlsxwriter.Workbook('output.xlsx')
for csvfile in csvfiles:
ws = wb.add_worksheet(os.path.split(csvfile)[-1][:30])
with open(csvfile,'rb') as f:
ur = UnicodeReader(f)
for r, row in enumerate(ur):
for c, col in enumerate(row):
ws.write(r, c, col)
wb.close()
read_csvandto_excelcan both easily accomplish what you're asking.