gpt4 book ai didi

excel - Python抛出 "' utf 8' codec can' t解码字节0xd0在位置0“错误

转载 作者:行者123 更新时间:2023-12-01 18:38:04 30 4
gpt4 key购买 nike

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

Excel 工作表:

enter image description here

文本文件:

enter image description here

我正在使用如下所示的代码:

# 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' codec can't decode byte 0xd0 in position 0: invalid continuation byte

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

最佳答案

Unicode 编码让我很困惑,但是你不能通过说以下内容强制该值忽略无效字节:

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

这里有一个很好的答案,可以让您进一步阅读有关 unicode 的内容:unicode().decode('utf-8', 'ignore') raising UnicodeEncodeError

关于excel - Python抛出 "' utf 8' codec can' t解码字节0xd0在位置0“错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19117836/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com