UnicodeDecodeError:“utf8”编解码器无法解码位置11中的字节0x80:无效的起始

2024-06-01 09:26:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用utf-8编码将数据写入excel表。 现在,我得到以下错误和完整的回溯-->

Traceback (most recent call last):
File "C:\Users\varun\Desktop\Python_testfiles\Reports      Automation\Txn.py", line 142, in <module>
domesticsheet.write(row, j, txn[payuid][j])
File "C:\Python27\lib\site-packages\xlwt\Worksheet.py", line 1030, in write
self.row(r).write(c, label, style)
File "C:\Python27\lib\site-packages\xlwt\Row.py", line 240, in write
StrCell(self.__idx, col, style_index, self.__parent_wb.add_str(label))
File "C:\Python27\lib\site-packages\xlwt\Workbook.py", line 326, in add_str
return self.__sst.add_str(s)
File "C:\Python27\lib\site-packages\xlwt\BIFFRecords.py", line 24, in add_str
s = unicode(s, self.encoding)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x80 in position 11:       invalid start byte

主要的问题是我随机得到这个错误。我运行了与前几天的数据对应的代码,运行得很好。 我尝试使用“utf-16”和“ascii”编码,而不是utf-8,但错误仍然存在(尽管错误语句已更改)

有什么方法可以消除这个错误吗?另外,我想知道为什么会出现这个错误(我是python的初学者)。任何帮助都将不胜感激。有必要提供一些编码类型吗?

如果需要查看代码,请执行以下操作-->

    filehandler[booknumber] = xlwt.Workbook(encoding = "utf-8")
    domesticsheet = filehandler[booknumber].add_sheet("Domestic_txn" + `booknumber`, cell_overwrite_ok=True)
    for k in range(len(header)):
        domesticsheet.write(0,k,header[k]);
    for j in range(len(txn[payuid])):
        domesticsheet.write(row, j, txn[payuid][j])

Tags: inpyselfaddlib错误linesite