Unicode Decode Error

Ian Kelly ian.g.kelly at gmail.com
Tue Oct 12 00:07:21 CEST 2010

On Mon, Oct 11, 2010 at 3:44 PM, Pratik Khemka <pratikkhemka at hotmail.com>wrote:

>  I tried to solve it using this *wbk = xlwt.Workbook('utf-8')... *The
> problem still persists. Is there a way I can solve this problem..?
> *Traceback (most recent call last):
>   File "C:\Documents and Settings\pkhemka\My Documents\merge_py.py", line
> 229, i
> n <module>
>     sheet.write(x,4,Technical_detail,datastyle)
>   File "C:\Python25\Lib\site-packages\xlwt\Worksheet.py", line 1003, in
> write
>     self.row(r).write(c, label, style)
>   File "C:\Python25\Lib\site-packages\xlwt\Row.py", line 231, in write
>     StrCell(self.__idx, col, style_index, self.__parent_wb.add_str(label))
>   File "C:\Python25\lib\site-packages\xlwt\Workbook.py", line 306, in
> add_str
>     return self.__sst.add_str(s)
>   File "C:\Python25\Lib\site-packages\xlwt\BIFFRecords.py", line 24, in
> add_str
>     s = unicode(s, self.encoding)
> UnicodeDecodeError: 'utf8' codec can't decode byte 0x92 in position 152:
> unexpec
> ted code byte*

This means that your data is also not utf-8.  That was just meant as an
example.  We have no way of knowing what the actual encoding of your data
is.  To find out, you need to check the source of your data and determine
what encoding it is being supplied in.  Failing that, you could try to
identify the actual character that is causing the problem and then work out
what encoding(s) denote that character starting with the byte 0x92.

If you just can't figure out the encoding, an alternative would be to decode
the strings before passing them into xlwt, using the 'ascii' codec and the
'ignore' or 'replace' error-handling scheme.  In doing so, you will lose the
problematic characters in your Excel workbook, but at least you won't have
to worry about this error any more.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20101011/a2702dc2/attachment.html>

More information about the Python-list mailing list