读取 excel 时出现 pandas 的断言错误

AssertionError with pandas when reading excel

我正在尝试使用 pandas.
将 xlsx 文件读入 python 我以前做过数千次,但由于某些原因它无法处理特定文件。

该文件是从其他来源下载的,在使用 pandas:

读取时出现 AssertionError(见结尾)
df = pandas.read_excel(pathtomyfile, sheetname = "Sheet1")

为路径定义变量。路径存在 (os.path.exists(path) returns True).

当我复制文件的内容并将值粘贴到一个新的 excel 文档中时,这个新文档将使用 read_excel() 方法打开。

当我复制文件内容并将格式粘贴到新的 excel 时, 这个新的将使用 read_excel() 方法打开。

它似乎不是值或格式。

我猜这可能是编码问题?
感谢您的帮助。

    df1 = pandas.read_excel(snap1)
File "C:\Python\python-3.4.4.amd64\lib\site-packages\pandas\io\excel.py", line 163, in read_excel
    io = ExcelFile(io, engine=engine)
File "C:\Python\python-3.4.4.amd64\lib\site-packages\pandas\io\excel.py", line 206, in __init__
    self.book = xlrd.open_workbook(io)
File "C:\Python\python-3.4.4.amd64\lib\site-packages\xlrd\__init__.py", line 422, in open_workbook
    ragged_rows=ragged_rows,
File "C:\Python\python-3.4.4.amd64\lib\site-packages\xlrd\xlsx.py", line 794, in open_workbook_2007_xml
    x12sheet.process_stream(zflo, heading)
File "C:\Python\python-3.4.4.amd64\lib\site-packages\xlrd\xlsx.py", line 531, in own_process_stream
    self_do_row(elem)
File "C:\Python\python-3.4.4.amd64\lib\site-packages\xlrd\xlsx.py", line 597, in do_row
    assert 0 <= self.rowx < X12_MAX_ROWS
AssertionError

为了完整起见,我遇到了类似的问题,第一行的行号不正确,我通过使用改编自 this answer

的代码更改 xlsx 文件来解决我的问题
def repair_broken_excelfile(zipfname, *filenames, new_name=None):
    # 
    import tempfile
    import zipfile
    import shutil
    import os
    tempdir = tempfile.mkdtemp()
    try:
        tempname = os.path.join(tempdir, 'new.zip')
        with zipfile.ZipFile(zipfname, 'r') as zipread:
            with zipfile.ZipFile(tempname, 'w') as zipwrite:
                for item in zipread.infolist():
                    print('fn: ' + item.filename)
                    if item.filename not in filenames:
                        data = zipread.read(item.filename)

                        zipwrite.writestr(item, data)
                    else:

                        data = zipread.read(item.filename)
                        data = data.replace(b'<row r="0" spans="">', b'<row r="1" spans="">')
                        zipwrite.writestr(item, data)
                        pass
        if not new_name:
            new_name = zipfname
        shutil.move(tempname, new_name)
    finally:
        shutil.rmtree(tempdir)

显然 xlrd

中有修复 underway

在我的例子中,我使用 xlrd 包读取 excel 并且我得到了相同的断言错误。 从站点包打开你的 xlrd 包,然后打开 sheet.py (https://github.com/python-excel/xlrd/blob/master/xlrd/sheet.py)

在 sheet.py

中找到此代码
    if self.biff_version >= 80:
        self.utter_max_rows = 65536
    else:
        self.utter_max_rows = 16384

把上面的转换成...

 #if self.biff_version >= 80:
 self.utter_max_rows = 65536
 #else:
 #      self.utter_max_rows = 16384

现在尝试 运行 您的程序... 问题将得到解决..:)

遇到了同样的问题,我在window上以xml格式保存文件:"Save as type: XML Spreadsheet 2003"。然后我打开文件并另存为 xlsx 格式。新文件不再给出错误信息。

该文件的文本中包含韩语字符。这些需要替代编码。 在 read_excel() 方法中使用 "encoding" 参数解决了问题。

df = pandas.read_excel(pathtomyfile, sheetname = "Sheet1", encoding="utf-16")

有时,只需删除 Excel 中 table 下面的(空白)行即可解决此问题。

在您的系统中查看文件 xlsx.py。

在您的计算机中,它显然位于 C:\Python\python-3.4.4.amd64\lib\site-packages\xlrd\xlsx.py

搜索行:

X12_MAX_ROWS = 2 ** 20

并将其更改为

X12_MAX_ROWS = 2 ** 22

这会将行数限制从 100 万行提高到 400 万行。