我正在尝试使用utf-8编码将数据写入excel工作表。现在我得到了以下错误与完整的回溯-->
Traceback (most recent call last):
File "C:UsersvarunDesktopPython_testfilesReports AutomationTxn.py", line 142, in <module>
domesticsheet.write(row, j, txn[payuid][j])
File "C:Python27libsite-packagesxlwtWorksheet.py", line 1030, in write
self.row(r).write(c, label, style)
File "C:Python27libsite-packagesxlwtRow.py", line 240, in write
StrCell(self.__idx, col, style_index, self.__parent_wb.add_str(label))
File "C:Python27libsite-packagesxlwtWorkbook.py", line 326, in add_str
return self.__sst.add_str(s)
File "C:Python27libsite-packagesxlwtBIFFRecords.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"one_answers"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])
0x80-0xBF范围内的字节在UTF-8编码中保留为延续字节。
0x00 - 0x7F - Single byte sequence, backwards compatible with ASCII
0x80 - 0xBF - Continuation byte for multi byte sequences
0xC0 - 0xDF - Starter byte for two byte sequence
0xE0 - 0xEF - Starter byte for three byte sequence
0xF0 - 0xF7 - Starter byte for four byte sequence
0xF8 - 0xFB - Starter byte for five byte sequence (overlong encoding)
0xFC - 0xFD - Starter byte for six byte sequence (overlong encoding)
0xFE - 0xFF - Illegal bytes
Python抱怨的是,您的数据在延续字节之前没有包含有效的起始字节。