【问题标题】:Trying to read data with excel pandas... and getting a consistent error across multiple files尝试使用 excel pandas 读取数据...并在多个文件中出现一致的错误
【发布时间】: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("####") 子句来证明这一点)

....我错过了什么?

【问题讨论】:

标签: pandas


【解决方案1】:

座椅和键盘之间的错误:

错误:

import pandas

file = 'KBART EDINA.xlsx'

with open(file, 'r') as xl_file:
    df = pandas.read_excel(xl_file)

import pandas

file = 'KBART EDINA.xlsx'

df = pandas.read_excel(file)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-20
    • 1970-01-01
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多