【问题标题】:Python Excel 'ascii' codec can't decodePython Excel'ascii'编解码器无法解码
【发布时间】:2019-07-11 19:25:16
【问题描述】:

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

代码目的很简单。有很多 excel 表格,其中包含大量信息,以及按特定顺序为每个表格重新排列列的内容。顺序基于顶部单元格,并且序列位于 "thelist" 中,如果由于某种原因它不存在,则仅创建一个空列,顶部单元格是列表中的字符串。这些表格包含很多来自我的国家字母表的外国符号,我就是无法超越

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

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

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

【问题讨论】:

  • 异常发生在哪里?什么线?
  • 已用错误消息对其进行了编辑。
  • 理论上应该是 UTF-8 ,但我不知道如何与我正在使用的 libre office 核实。
  • 我试过了,只是在另一篇文章看到后才改成 encoding_override,但没有任何改变
  • just ,encoding = 'utf-8' 给出了错误(open_workbook() 得到了一个意外的关键字参数 'encoding')

标签: python excel xlrd xlwt


【解决方案1】:

根据xlwt's documentation Workbook 构造函数接受encoding 参数。

你应该尝试改变:

workbook2 = xlwt.Workbook()

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

【讨论】:

    猜你喜欢
    • 2017-03-01
    • 1970-01-01
    • 2015-11-06
    • 2011-06-29
    • 1970-01-01
    • 2013-08-20
    • 2013-09-09
    • 2020-07-17
    • 2018-05-05
    相关资源
    最近更新 更多