so I have a script that is taking input from a text file and using it to populate an excel spreadsheet. it keeps giving me a UnicodeDecodeError, dying on the ± symbol, I'm assuming its not in the codec that is being used:

im using the xlwt package for writign to excel.

workBook.save((self.output))
  File "C:\Python25\lib\site-packages\xlwt\Workbook.py", line 634, in save
    doc.save(filename, self.get_biff_data())
  File "C:\Python25\lib\site-packages\xlwt\Workbook.py", line 609, in get_biff_data
    shared_str_table   = self.__sst_rec()
  File "C:\Python25\lib\site-packages\xlwt\Workbook.py", line 571, in __sst_rec
    return self.__sst.get_biff_record()
  File "C:\Python25\Lib\site-packages\xlwt\BIFFRecords.py", line 53, in get_biff_record
    self._add_to_sst(s)
  File "C:\Python25\Lib\site-packages\xlwt\BIFFRecords.py", line 66, in _add_to_sst
    u_str = upack2(s, self.encoding)
  File "C:\Python25\Lib\site-packages\xlwt\UnicodeUtils.py", line 50, in upack2
    us = unicode(s, encoding)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb1 in position 59: ordinal not in range(128)

I've tried to encode it but unsure which codec to use. any recommendations for how to handle it?

the code that dies is when it saves the workbook with that symbol in the line

It seems to be trying to use ascii for some reason and not unicode. What is the encoding variable set to? Setting it to utf-8 should work.

You might like this guide to unicode in Python 2 - http://farmdev.com/talks/unicode/

Edited 7 Years Ago by god0fgod: n/a

This article has been dead for over six months. Start a new discussion instead.