Santanu C
Santanu C

Reputation: 1402

UnicodeDecodeError error writing .xlsx file using xlsxwriter

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?

Upvotes: 11

Views: 12408

Answers (2)

jmcnamara
jmcnamara

Reputation: 41664

As Alex Hristov points out you have some non-ascii data in your code that needs to be encoded as UTF-8 for Excel.

See the following examples from the docs which each have instructions on handling UTF-8 with XlsxWriter in different scenarios:

Upvotes: 4

Alexander Ejbekov
Alexander Ejbekov

Reputation: 5970

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.

Upvotes: 17

Related Questions