11

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?

jmcnamara
  • 38,196
  • 6
  • 90
  • 108
Santanu C
  • 1,362
  • 3
  • 20
  • 38

2 Answers2

17

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.

Alexander Ejbekov
  • 5,594
  • 1
  • 26
  • 26
4

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:

jmcnamara
  • 38,196
  • 6
  • 90
  • 108