Reputation: 11
score_sheet = [[2],[1],[0],[0],[6],[0],[0],[1],[0],[2],[1],[1],[4],[0], [0],[0, 'W', 'S Dhawan c \xe2\x80\xa0Uthappa b Morkel 6 '],[1],[0],[4],[0, 'W', 'DA Warner c SA Yadav b UT Yadav 13 '],[0],[2],[0],[1],[1],[0],[0],[1],[2],[0],[4],[0],[1],[0],[1],[0, 'W', 'MC Henriques lbw b UT Yadav 6 '],[0],[1],[1],[1],[1],[1],[1],[0],[0],[0],[1],[0],[1],[0],[1],[1],[0],[2],[1],[1],[0],[0, 'W', 'DJ Hooda c UT Yadav b Russell 6 '],[2],[0],[1],[1],[0],[0],[1],[0],[2],[2],[0],[4],[0],[0],[1],[0],[1],[4],[4],[0],[4],[0],[3],[2],[0],[4],[0],[1],[1],[6],[0],[0],[1],[1],[1],[0],[6],[2],[1],[1],[0],[1],[1],[6],[1],[1],[0, 'W', 'NV Ojha c Chawla b Morkel 37 '],[1],[1],[6],[1],[1],[1],[0, 'W', 'EJG Morgan c Shakib Al Hasan b UT Yadav 51 '],[1],[6],[1],[1],[1],[2],[1, 'W', 'A Ashish Reddy run out 13 '],[1]]
import xlwt
from tempfile import TemporaryFile
book = xlwt.Workbook()
sheet1 = book.add_sheet('sheet1')
for i,e in enumerate(score_sheet):
if len(score_sheet[i])==1:
sheet1.write(i,1,e[0])
else:
sheet1.write(i,1,e[0])
sheet1.write(i,2,e[1])
sheet1.write(i,3,e[2])
n = "score_book.xls"
book.save(n)
book.save(TemporaryFile())
I want to convert score_sheet into excel file And it is showing these errors:
File "", line 2, in book.save(name)
File "C:\Python27\lib\site-packages\xlwt\Workbook.py", line 710, in save doc.save(filename_or_stream, self.get_biff_data())
File "C:\Python27\lib\site-packages\xlwt\Workbook.py", line 674, in get_biff_data shared_str_table = self.__sst_rec()
File "C:\Python27\lib\site-packages\xlwt\Workbook.py", line 636, in __sst_rec return self.__sst.get_biff_record()
File "C:\Python27\lib\site-packages\xlwt\BIFFRecords.py", line 77, in get_biff_record self._add_to_sst(s)
File "C:\Python27\lib\site-packages\xlwt\BIFFRecords.py", line 92, in _add_to_sst u_str = upack2(s, self.encoding)
File "C:\Python27\lib\site-packages\xlwt\UnicodeUtils.py", line 50, in upack2 us = unicode(s, encoding)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 11: ordinal not in range(128)
Can you please guide me what should I do?
Upvotes: 1
Views: 384
Reputation: 308081
You're using non-Unicode strings in a function that wants Unicode strings. Python 2 will attempt to do the conversion automatically, but it uses the 'ascii'
codec which doesn't handle all the characters.
Replace:
'S Dhawan c \xe2\x80\xa0Uthappa b Morkel 6 '
with:
u'S Dhawan c \u2020Uthappa b Morkel 6 '
Upvotes: 3