0

I am using python 2.7.9 to create excel sheet using tab delimited text files; however I am getting problem while running this python script

    #!/usr/bin/env python
    # encoding=utf8  
    import xlwt
    import os   
    import sys  
    reload(sys)  
    sys.setdefaultencoding('utf8')


    wb = xlwt.Workbook()
    path = "/home/Final_analysis/"
    #print(os.listdir())
    lis = os.listdir(path)
    sheetnumber = 1
    for x in lis:
        if os.path.isfile(x)==True:
            extension = os.path.splitext(x)
            print(extension[1])
            if extension[1] == '.txt':
                #print("Yes")
                ws = wb.add_sheet(extension[0])
                row = 0
                column = 0
                a = open(x)

                while True:
                    a1 = a.readline()
                    if len(a1)==0:
                        break
                    data = a1.split("\t")
                    for z in data:
                        ws.write(row,column,z)
                        column += 1
                    column = 0
                    row += 1
                sheetnumber+=1
            else:
                pass
    wb.save("Ronic.xls")

I am getting following error

Traceback (most recent call last):
  File "home/Final_analysis/combine_excel_v2.py", line 39, in <module>
    wb.save("Ronic.xls")
  File "/usr/local/lib/python2.7/site-packages/xlwt/Workbook.py", line 710, in save
    doc.save(filename_or_stream, self.get_biff_data())
  File "/usr/local/lib/python2.7/site-packages/xlwt/Workbook.py", line 674, in get_biff_data
    shared_str_table   = self.__sst_rec()
  File "/usr/local/lib/python2.7/site-packages/xlwt/Workbook.py", line 636, in __sst_rec
    return self.__sst.get_biff_record()
  File "/usr/local/lib/python2.7/site-packages/xlwt/BIFFRecords.py", line 77, in get_biff_record
    self._add_to_sst(s)
  File "/usr/local/lib/python2.7/site-packages/xlwt/BIFFRecords.py", line 92, in _add_to_sst
    u_str = upack2(s, self.encoding)
  File "/usr/local/lib/python2.7/site-packages/xlwt/UnicodeUtils.py", line 50, in upack2
    us = unicode(s, encoding)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 83: ordinal not in range(128)

I have used answer given in thread How to fix: "UnicodeDecodeError: 'ascii' codec can't decode byte"

But it didn't work.

problem is at wb.save() command

RonicK
  • 229
  • 2
  • 3
  • 10

1 Answers1

2

Setting the encoding at the top of your program is to handle non-ascii characters in your code, not your data. sys.setdefaultencoding('utf8') is not intended to be used in ordinary programs and does more harm than good.

To fix the problem, tell xlwt about the encoding to use.

Change this line:

wb = xlwt.Workbook()

to this:

wb = xlwt.Workbook(encoding="UTF-8")
BoarGules
  • 16,440
  • 2
  • 27
  • 44