UnicodeDecodeError error writing .xlsx file using xlsxwriter

Santanu C picture Santanu C · Jan 16, 2014 · Viewed 11.5k times · Source

I am trying to write about 1000 rows to a .xlsx file from my python application. The data is basically a combination of integers and strings. I am getting intermittent error while running wbook.close() command. The error is the following:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 15: 
                     ordinal not in range(128)

My data does not have anything in unicode. I am wondering why the decoder is being at all. Has anyone noticed this problem?

Answer

Alexander Ejbekov picture Alexander Ejbekov · Jan 16, 2014

0xc3 is "À". So what you need to do is change the encoding. Use the decode() method.

string.decode('utf-8')

Also depending on your needs and uses you could add

# -*- coding: utf-8 -*-

at the beginning of your script, but only if you are sure that the encoding will not interfere and break something else.