1 python export-to-excel python-2.7
我正在尝试使用 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
Run Code Online (Sandbox Code Playgroud)
主要问题是我随机收到此错误。我运行了前几天对应的数据代码,运行得很好。我尝试使用“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])
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
Python 抱怨的是你的数据在连续字节之前不包含有效的起始字节。