问题
I am trying to read an xlsb file from local using pandas' read_excel but I am getting error. My code:
import pandas as pd
df3 = pd.read_excel('a.xlsb', engine = 'pyxlsb')
Error:
---------------------------------------------------------------------------
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
It works fine for csv and xlsx files.
python version: 3.5.2
pandas version: 0.24.2
回答1:
First install pyxlsb and run the below code.After running the code, you'll have your data stored in 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])
回答2:
After looking into the problem a bit more and referring to @Datanovice 's comment, it works for me if I update to pandas v1.0. I am using ubuntu 16.04 which can automatically update my python to 3.5, not any further and pandas v1.0 is supported from python 3.6. Hence, even after updating with the latest versions, I was not able to run the code. We can install python 3.6 and install pandas v1.0 for that.
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.6
Using pandas 3.6, we can simply pass the engine as pyxlsb to read_excel to read the file.
import pandas as pd
df3 = pd.read_excel('a.xlsb', engine = 'pyxlsb')
Reference to install python3.6 on Ubuntu 16.04: https://askubuntu.com/questions/865554/how-do-i-install-python-3-6-using-apt-get
来源:https://stackoverflow.com/questions/60152337/unable-to-read-xlsb-file-using-pandas