Python的投掷“'utf8'编解码器无法解码位置0字节0xd0”错误
我正在尝试加载当前存在的工作表并导入下面显示的文本文件(逗号分隔值)截图,
Excel工作表:
文本文件:
我正在使用下面显示的代码:
# 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:无效的继续字节
还有一个问题:如何告诉python从excel表格的A3列开始导入文本数据?
Unicode编码混淆了我,但不能强制值忽略无效字节说:
value = unicode(value, errors='ignore')
unicode()。decode('utf-8','ignore')引发UnicodeEncodeError
openpyxl只处理OOXML格式(xlsx / xlsm)。 请尝试使用Excel保存为xlsx文件格式而不是xls。
如果你想在代码中将xls文件转换为xlsx文件。 请从下面的列表中尝试一个选项:
- 在Windows中,您也可以使用excelcnv工具将xls转换为xlxx。
- 在Linux中,请检查这篇文章 。
- 或者,您可以在Python中使用xlrd将其转换为xlsx。 请检查这个问答 。
嗨你确定你没有一个具有UTF-8 BOM的文档
您可以尝试使用UTF-8 BOM编解码器 。 一般Windows + UTF + 8可能有点麻烦。 虽然它显示的字符可能不是BOM。