无法使用 pandas 读取 xlsb 文件

Unable to read xlsb file using pandas

我正在尝试使用 pandas' read_excel 从本地读取 xlsb 文件,但出现错误。 我的代码:

import pandas as pd
df3 = pd.read_excel('a.xlsb', engine = 'pyxlsb')


错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-17-06db88cb2446> in <module>
----> 1 pd.read_excel('a.xlsb', engine='pyxlsb')

/usr/local/lib/python3.5/dist-packages/pandas/util/_decorators.py in wrapper(*args, **kwargs)
    186                 else:
    187                     kwargs[new_arg_name] = new_arg_value
--> 188             return func(*args, **kwargs)
    189         return wrapper
    190     return _deprecate_kwarg

/usr/local/lib/python3.5/dist-packages/pandas/util/_decorators.py in wrapper(*args, **kwargs)
    186                 else:
    187                     kwargs[new_arg_name] = new_arg_value
--> 188             return func(*args, **kwargs)
    189         return wrapper
    190     return _deprecate_kwarg

/usr/local/lib/python3.5/dist-packages/pandas/io/excel.py in read_excel(io, sheet_name, header, names, index_col, parse_cols, usecols, squeeze, dtype, engine, converters, true_values, false_values, skiprows, nrows, na_values, keep_default_na, verbose, parse_dates, date_parser, thousands, comment, skip_footer, skipfooter, convert_float, mangle_dupe_cols, **kwds)
    348 
    349     if not isinstance(io, ExcelFile):
--> 350         io = ExcelFile(io, engine=engine)
    351 
    352     return io.parse(

/usr/local/lib/python3.5/dist-packages/pandas/io/excel.py in __init__(self, io, engine)
    644             engine = 'xlrd'
    645         if engine not in self._engines:
--> 646             raise ValueError("Unknown engine: {engine}".format(engine=engine))
    647 
    648         # could be a str, ExcelFile, Book, etc.

ValueError: Unknown engine: pyxlsb

它适用于 csv 和 xlsx 文件。

python版本:3.5.2
pandas 版本:0.24.2

首先安装 pyxlsb 和 运行 下面的 code.After 运行 代码,您的数据将存储在 df1 中。

pip install pyxlsb

import pandas as pd
from pyxlsb import open_workbook

df=[]
with open_workbook('some.xlsb') as wb:
    with wb.get_sheet(1) as sheet:
        for row in sheet.rows():
            df.append([item.v for item in row])

df1 = pd.DataFrame(df[1:], columns=df[0])

在进一步研究问题并参考@Datanovice 的评论后,如果我更新到 pandas v1.0,它对我有用。 我正在使用 ubuntu 16.04,它可以自动将我的 python 更新到 3.5,不再更新 pandas v1.0 从 python 3.6 开始支持。因此,即使在更新了最新版本之后,我也无法 运行 代码。 我们可以安装 python 3.6 并为此安装 pandas v1.0。

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.6

使用pandas 3.6,我们可以简单地将引擎作为pyxlsb传递给read_excel来读取文件。

import pandas as pd
df3 = pd.read_excel('a.xlsb', engine = 'pyxlsb')

参考在 Ubuntu 16.04 上安装 python3.6:https://askubuntu.com/questions/865554/how-do-i-install-python-3-6-using-apt-get