Python抛出“'utf8'编解码器无法解码位置0的字节0xd0";错误 [英] Python Throwing "'utf8' codec can't decode byte 0xd0 in position 0" Error

查看:939
本文介绍了Python抛出“'utf8'编解码器无法解码位置0的字节0xd0";错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试加载当前存在的工作表并导入下面显示的文本文件(以逗号分隔的值)的屏幕截图,

I am trying to load a currently existing worksheet and import the text file (comma separated values) screenshot shown below,

Excel工作表:

文本文件:

我正在使用下面显示的代码:

I am using the code shown below:

# importing necessary modules for performing the required operation
    import glob
    import csv
    from openpyxl import load_workbook
    import xlwt

    #read the text file(s) using the CSV modules and read the dilimiters and quoutechar
    for filename in glob.glob("E:\Scripting_Test\Phase1\*.txt"):
        spamReader = csv.reader((open(filename, 'rb')), delimiter=',')


        #read the excel file and using xlwt modules and set the active sheet
        wb = load_workbook(filename=r"E:\Scripting_Test\SeqTem\Seq0001.xls")
        ws = wb.worksheets(0)


        #write the data that is in text file to excel file
        for rowx, row in enumerate(spamReader):
            for colx, value in enumerate(row):
                ws.write(rowx, colx, value)

        wb.save()

我收到以下错误消息:

UnicodeDecodeError:'utf8'编解码器无法解码位置0处的字节0xd0:无效的连续字节

UnicodeDecodeError: 'utf8' codec can't decode byte 0xd0 in position 0: invalid continuation byte

另一个问题:如何告诉python从Excel工作表中的A3列开始导入文本数据?

One more question: How can you tell python to import the text data starting from A3 column in the excel sheet?

推荐答案

Unicode编码使我感到困惑,但是您不能通过说:

Unicode encoding confuses me, but can't you force the value to ignore invalid bytes by saying:

value = unicode(value, errors='ignore')

以下是有关Unicode的更多阅读的好答案: unicode().decode( 'utf-8','ignore')引发UnicodeEncodeError

Here is a great answer for more reading on unicode: unicode().decode('utf-8', 'ignore') raising UnicodeEncodeError

这篇关于Python抛出“'utf8'编解码器无法解码位置0的字节0xd0";错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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