Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何根据输入文件扩展名更改pd.read\u excel()引擎?_Python_Excel_Pandas - Fatal编程技术网

Python 如何根据输入文件扩展名更改pd.read\u excel()引擎?

Python 如何根据输入文件扩展名更改pd.read\u excel()引擎?,python,excel,pandas,Python,Excel,Pandas,我使用以下代码将工作表中的数据读入数据帧: binary_file = requests.get(url_to_excel_file).content workbook = pd.ExcelFile(binary_file) # then get the dataframes from the multiple sheets i need df1 = pd.read_excel(workbook, sheet_name = sheet_name_A, index_col=None) df2 =

我使用以下代码将工作表中的数据读入数据帧:

binary_file = requests.get(url_to_excel_file).content
workbook = pd.ExcelFile(binary_file)
# then get the dataframes from the multiple sheets i need
df1 = pd.read_excel(workbook, sheet_name = sheet_name_A, index_col=None)
df2 = pd.read_excel(workbook, sheet_name = sheet_name_B, index_col=None)
然而,在最新的xlrd库中,由于某种原因,对XLSX的支持被删除

问题:
考虑到我可能在url中收到xls或xlsx,我如何获得df1和df2?

答案

根据文件类型,在ExcelFile构造函数中设置引擎

if extension == "xls":
    pd.ExcelFile(binary_file, engine = "xlrd")
elif extension == "xlsx":
    pd.ExcelFile(binary_file, engine = "openpyxl")

xlrd将只处理xls文件。对xlsx的支持已撤销。您可以通过删除一个版本来使用pandas读取xlsx文件。