UnicodeDecodeError:'utf8'编解码器无法解码位置11的字节0x80:无效的起始字节 [英] UnicodeDecodeError: 'utf8' codec can't decode byte 0x80 in position 11: invalid start byte

查看:265
本文介绍了UnicodeDecodeError:'utf8'编解码器无法解码位置11的字节0x80:无效的起始字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用utf-8编码将数据写入excel工作表. 现在我得到以下错误,具有完整的追溯->

I am trying to write data into an excel sheet using the utf-8 encoding. Right now i get the following error with complete traceback -->

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,但错误仍然存​​在(但错误声明已更改.)

The main issue is that i get this error randomly. I ran the code for data corresponding to some other day and it ran just fine. I tried using "utf-16" and "ascii" encoding as well instead of utf - 8 but the error persists(the error statement changed, though.)

有什么办法可以消除此错误?另外,我想知道为什么会出现此错误(我是python的初学者).任何帮助将不胜感激.甚至需要提供某种编码类型吗?

Is there some way i can get rid of this error? Also, i would like to know why this error comes(I am a beginner at python). Any help will be highly appreciated. Is it necessary to even provide some encoding type?

如果您需要查看代码,则如下所示->

If you need to see the code it is as follows-->

    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编码形式保留为连续字节.

Bytes in the range 0x80 - 0xBF are reserved in UTF-8 encoding as continuation bytes.

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

What Python is complaining about is that your data doesn't contain a valid starter byte before a continuation byte.

这篇关于UnicodeDecodeError:'utf8'编解码器无法解码位置11的字节0x80:无效的起始字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆