Python Excel' ascii'编解码器无法解码

时间:2016-06-23 14:46:26

标签: python excel xlrd xlwt

我知道有很多这些,我已经阅读过,只是不知道如何修复它所以我在这里发布另一个。

代码目的很简单。有很多excel表,包含大量信息,以及按照特定顺序为每个列重新排列列的内容。订单基于顶部单元格,顺序在" thelist" ,如果由于某种原因它不存在只是创建一个空列,顶部单元格列表中的字符串。这些表格包含了我国alplabet的很多外国符号,我只是无法通过

' ASCII'编解码器不能解码位置6中的字节0xe2:序数不在范围内(128)

无论如何这里是代码:

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')

这可能是一个非常简单的改变,但对于我的生活,我尝试添加.encode和.decode与不同的utf,但它只是不起作用。任何帮助将不胜感激。

<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)

1 个答案:

答案 0 :(得分:0)

根据xlwt's documentationWorkbook构造函数采用一个encoding参数。

您应该尝试更改:

workbook2 = xlwt.Workbook()

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