简体   繁体   English

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

[英]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. 有很多具有大量信息的excel表,以及如何按每个表的特定顺序重新排列列。 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. 顺序基于顶部单元格,并且顺序在“ thelist”中,如果由于某种原因而不是,则仅使用顶部单元格中的字符串创建一个空列。 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) 'ascii'编解码器无法解码位置6的字节0xe2:序数不在范围内(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. 这可能是一个非常简单的更改,但是对于我来说,我尝试使用不同的utf添加.encode和.decode,但它不起作用。 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. 根据xlwt的文档Workbook构造函数采用encoding参数。

You should try to change: 您应该尝试更改:

workbook2 = xlwt.Workbook()

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:“ ascii”编解码器无法解码字节 - Python: 'ascii' codec can't decode byte Python - 'ascii'编解码器无法解码字节 - Python - 'ascii' codec can't decode byte Python - “ascii”编解码器无法解码字节 - Python - 'ascii' codec can't decode byte UnicodeDecodeError:“ ascii”编解码器无法解码-Python - UnicodeDecodeError: 'ascii' codec can't decode - Python python熊猫到excel UnicodeDecodeError:&#39;ascii&#39;编解码器无法解码位置11的字节0xe2 - Python pandas to excel UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 11 Python 2.7 UnicodeDecodeError:&#39;ascii&#39;编解码器无法解码字节 - Python 2.7 UnicodeDecodeError: 'ascii' codec can't decode byte Python(nltk)-UnicodeDecodeError:“ ascii”编解码器无法解码字节 - Python (nltk) - UnicodeDecodeError: 'ascii' codec can't decode byte Python连接字符串-UnicodeDecodeError:&#39;ascii&#39;编解码器无法解码字节 - Python concatenating strings - UnicodeDecodeError: 'ascii' codec can't decode byte Python 3 UnicodeDecodeError:“ascii”编解码器无法解码字节 0xc2 - Python 3 UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 Python - 'ascii'编解码器无法解码byte \ xbd的位置 - Python - 'ascii' codec can't decode byte \xbd in position
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM