简体   繁体   中英

Python Excel 'ascii' codec can't decode

I know there are a lot of these , I have read them , just don't know how to fix it so here I am posting another one.

Code purpose is simple. Have a lot of excel tables with a lot of information and what to re-arrange the columns in a certain order for each one. Order is based on the top cell and the sequence is in "thelist" , if for some reason its not there just creates an empty column with the top cell the string from the list. The tables contain a lot of foreign symbols from my countries alplabet and I just can't get past that

'ascii' codec can't decode byte 0xe2 in position 6: ordinal not in range(128)

Anyway here is the code:

import xlwt
import xlrd
import codecs
from transliterate import translit, get_available_language_codes

thelist = ["has 46 string elements so no point in pasting the whole thing"]
l_thelist = len(thelist) #46 or something like that



workbook = xlrd.open_workbook('input.xls')
active_sheet = workbook.sheet_by_index(0)

data = [active_sheet.cell_value(0, col) for col in range(active_sheet.ncols)]



num_rows = active_sheet.nrows
num_cols = active_sheet.ncols




workbook2 = xlwt.Workbook()
second_sheet= workbook2.add_sheet('test')

#0->num_rows,i,
#0-5 always the same 
for i in range(0,6):
    for j in range(0,num_rows):
        second_sheet.write(j,i,active_sheet.cell_value(j,i))


my_col=6 # start from 6 cause the first five must always be the same 
# this integer is for column number
for x in range(0,l_thelist):
    counter = 7;
    for i in range(6,num_cols): # i would be column in the first file
        if active_sheet.cell_value(0,i)==thelist[x]:
            for z in range(0,num_rows):
                second_sheet.write(z,my_col,active_sheet.cell_value(z,i))


            my_col=my_col + 1
            counter=0 #this is to stop duplication at the end of the loop
        else:
            counter = counter + 1 

        if counter>num_cols: #if its not on the lis create a an empty table with the top cell string from the list 
            second_sheet.write(0,my_col,thelist[x])
            counter=0
            my_col = my_col + 1


#adding the ones not on the list after 
col_2=8 + len(thelist)

for i in range(6,num_cols):
    counter = 0 
    for x in range(0,l_thelist):

        if active_sheet.cell_value(0,i)!=thelist[x] and counter==l_thelist-1:
                    for z in range(0,num_rows):
                        second_sheet.write(z,col_2,active_sheet.cell_value(z,i))

                    col_2=col_2 + 1
        else:
            counter=counter+1



#for x in range(0,l_thelist):
    #for i in range(6,num_cols):


workbook2.save('output.xls')

Its probably a very simple change but for the life of me I've tried adding .encode and .decode with the different utf's but it just doesn't work. Any help will be appreciated.

<ipython-input-12-174e461d09df> in <module>()
     74 
     75 
---> 76 workbook2.save('output.xls')
     77 
     78 

C:\Anaconda\lib\site-packages\xlwt\Workbook.pyc in save(self, filename_or_stream)
    694 
    695         doc = CompoundDoc.XlsDoc()
--> 696         doc.save(filename_or_stream, self.get_biff_data())
    697 
    698 

C:\Anaconda\lib\site-packages\xlwt\Workbook.pyc in get_biff_data(self)
    658         all_links          = self.__all_links_rec()
    659 
--> 660         shared_str_table   = self.__sst_rec()
    661         after = country + all_links + shared_str_table
    662 

C:\Anaconda\lib\site-packages\xlwt\Workbook.pyc in __sst_rec(self)
    620 
    621     def __sst_rec(self):
--> 622         return self.__sst.get_biff_record()
    623 
    624     def __ext_sst_rec(self, abs_stream_pos):

C:\Anaconda\lib\site-packages\xlwt\BIFFRecords.pyc in get_biff_record(self)
     75                 s = u''
     76             if isinstance(s, basestring):
---> 77                 self._add_to_sst(s)
     78             else:
     79                 self._add_rt_to_sst(s)

C:\Anaconda\lib\site-packages\xlwt\BIFFRecords.pyc in _add_to_sst(self, s)
     90 
     91     def _add_to_sst(self, s):
---> 92         u_str = upack2(s, self.encoding)
     93 
     94         is_unicode_str = u_str[2] == b'\x01'

C:\Anaconda\lib\site-packages\xlwt\UnicodeUtils.pyc in upack2(s, encoding)
     48         us = s
     49     else:
---> 50         us = unicode(s, encoding)
     51     # Limit is based on number of content characters
     52     # (not on number of bytes in packed result)

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 6: ordinal not in range(128)

According to xlwt's documentation the Workbook constructor take an encoding parameter.

You should try to change:

workbook2 = xlwt.Workbook()

to

workbook2 = xlwt.Workbook(encoding="utf-8")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM