使用 python pandas 打开旧格式的 xls 文件时遇到困难

问题描述 投票:0回答:1

我有一个 Microsoft Excel 97-2003 xls 格式的电子表格。我尝试了以下方法:

import pandas as pd

xlsx_file_path = "C:/temp/a_file.xls"
sheets_dict = pd.read_excel(xlsx_file_path, engine='xlrd', sheet_name=None)

for sheet_name, df_in in sheets_dict.items():
    print(sheet_name)

它给出错误:

  File C:\xxxxxx\site-packages\xlrd\__init__.py:172 in open_workbook
    bk = open_workbook_xls(

  File C:\xxxxxxx\site-packages\xlrd\book.py:79 in open_workbook_xls
    biff_version = bk.getbof(XL_WORKBOOK_GLOBALS)

  File C:\xxxxxxxx\site-packages\xlrd\book.py:1284 in getbof
    bof_error('Expected BOF record; found %r' % self.mem[savpos:savpos+8])

  File C:\xxxxxxxx\site-packages\xlrd\book.py:1278 in bof_error
    raise XLRDError('Unsupported format, or corrupt file: ' + msg)

XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'\xef\xbb\xbf\xef\xbb\xbf<?'

我尝试了其他引擎,例如 openpyxl,并收到以下错误:

  File C:\xxxx\lib\zipfile.py:1336 in _RealGetContents
    raise BadZipFile("File is not a zip file")

BadZipFile: File is not a zip file

有什么解决办法吗?

python pandas excel openpyxl xlrd
1个回答
0
投票

XLS 文件是非压缩文件格式的 birany 文件。 因为你需要这样使用

import pandas as pd

# Change the file path .xls
file_path = 'in.xls'

# Read the Old Excel file .xls using the method `read_excel` of Pandas
sheets_dict = pd.read_excel(file_path)

# Printing the data
print(sheets_dict.head())
© www.soinside.com 2019 - 2024. All rights reserved.