繁体   English   中英

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

[英]UnicodeDecodeError: 'utf8' codec can't decode byte 0x80 in position 11: invalid start byte

我正在尝试使用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])

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抱怨的是您的数据在连续字节之前不包含有效的起始字节。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM