I have a python script that is using xlwt/xlrd to work with an excel file. at the beginning of my script I have the following code:
#if you got a csv in parameters, convert it to an xls file
if '.csv' in sys.argv[1]:
#name of new file after conversion is finished
name = sys.argv[1]
csvfile = open(sys.argv[1], 'rb')
try:
#extract data from .csv
csvReader = csv.reader(csvfile, delimiter=' ', quotechar='|')
csvData = list(csv.reader(open(name, 'rb')))
# write to a xls file
outFile = xlwt.Wrokbook()
newSheet = outFile.add_sheet('Sheet 1')
# traverse over 2d array to write each individual cell
for row in range(len(csvData)):
for col in range(len(csvData[0])):
newSheet.write(row, col, csvData[row][col].encode('utf8'))
name = name[:-4] + ".xls" #change extension of file
outFile.save(name)
wb = open_workbook(name)
finally:
csvfile.close()
Which gives the error
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 44: ordinal not in range(128)
on the line outFile.save(name)
The only useful thing I've found so far to help with this is UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 1, but my terminal is using utf8 as its encoding.
EDIT: Totally forgot to mention this, so sorry.
I believe that the line with .encode is causing the error somehow but I can't think how. I originally had it without the .encode, then i added .encode('utf8'), also tried .encode('utf-8') and unicode(string, 'utf8'). I'm not sure what else to try to solve this problem.
EDIT: I tried Brian's suggestion to no avail. Additionally I attempted the codecs.open suggestion and also tried specifying the encoding when I create the workbook. None of these things change the error at all. The only thing I've tried that changes the error is the adding of the .encode on the line with newSheet.write. Without it I get:
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position 44: ordinal no in range(128)
And with it I get:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 44: ordinal not in range(128)