【发布时间】:2021-06-21 14:54:31
【问题描述】:
这是我的测试代码,它给出了我的错误:
import pandas
file = 'KBART EDINA.xlsx'
data = list()
with open(file, 'r') as xl_file:
df = pandas.read_excel(xl_file)
当我运行它时,我收到以下错误:
❯ python hack.py
Traceback (most recent call last):
File "hack.py", line 11, in <module>
df = pandas.read_excel(xl_file)
File "/CWD/deliberately/obscured/.direnv/python-3.7.6/lib/python3.7/site-packages/pandas/util/_decorators.py", line 299, in wrapper
return func(*args, **kwargs)
File "/CWD/deliberately/obscured/.direnv/python-3.7.6/lib/python3.7/site-packages/pandas/io/excel/_base.py", line 336, in read_excel
io = ExcelFile(io, storage_options=storage_options, engine=engine)
File "/CWD/deliberately/obscured/.direnv/python-3.7.6/lib/python3.7/site-packages/pandas/io/excel/_base.py", line 1072, in __init__
content=path_or_buffer, storage_options=storage_options
File "/CWD/deliberately/obscured/.direnv/python-3.7.6/lib/python3.7/site-packages/pandas/io/excel/_base.py", line 954, in inspect_excel_format
buf = stream.read(PEEK_SIZE)
File "/another/obscured/path/miniconda3/lib/python3.7/codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb9 in position 16: invalid start byte
我尝试了 6 个不同的 xls、xlsx 和 ods 文件.....它们都返回相同的错误
我安装了以下(相关)库:
openpyxl 3.0.7
pandas 1.2.4
xlrd 2.0.1
我知道文件是可读的(我有一个if not os.path.isfile(file): print("####") 子句来证明这一点)
....我错过了什么?
【问题讨论】:
-
错误不是文件句柄是否可读。发生错误是因为在读取时遇到了无效的代码点。想象一个数字密码,其中 A -> 1、B -> 2 等。如果输入为零,你会得到什么字母?这基本上就是
UnicodeDecodeError在这里的意思。 -
这能回答你的问题吗? Pandas read _excel: 'utf-8' codec can't decode byte 0xa8 in position 14: invalid start byte。我知道具体的字节是不同的。根据stackoverflow.com/a/53123720/2741091,查看用原始路径替换输入文件句柄是否有帮助。
标签: pandas