【问题标题】:AssertionError with pandas when reading excel阅读excel时大熊猫的AssertionError
【发布时间】:2016-09-25 23:24:14
【问题描述】:

我正在尝试使用 pandas 将 xlsx 文件读入 python。
我之前已经这样做了数千次,但由于某种原因它不适用于特定文件。

文件是从另一个来源下载的,我在使用 pandas 阅读时收到 AssertionError(见末尾):

df = pandas.read_excel(pathtomyfile, sheetname = "Sheet1")

变量是为路径定义的。路径存在(os.path.exists(path) 返回 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

【问题讨论】:

  • 基于断言检查是0 &lt;= self.rowx &lt; X12_MAX_ROWS 我猜rowx 要么是负数(我知道为什么会发生这种情况),要么比X12_MAX_ROWS 更重要,你的电子表格是否显着大吗?
  • 不只是82行长,三列
  • 那么文件中是否有可能存在超出范围的杂散值?如果您备份内容,全选并删除,然后再粘贴回内容怎么办?
  • 当我复制到另一个 excel 文件时,我选择了所有.....当我尝试 read_table 方法时,它说的是找到了 0x89?这可能是错误的来源吗?
  • 报错如下:UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte

标签: python excel pandas xlsx xlrd


【解决方案1】:

在我的例子中,我使用 xlrd 包来读取 excel,我得到了同样的 Assertion 错误。 从 site-packages 和打开的 sheet.py (https://github.com/python-excel/xlrd/blob/master/xlrd/sheet.py) 中打开您的 xlrd 包

在 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

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

【讨论】:

  • 对这里发生的情况/情况有任何了解吗?
【解决方案2】:

在您的系统中查找文件 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 万行。

【讨论】:

    【解决方案3】:

    为了完整起见,我遇到了类似的问题,第一行的行号不正确,我通过使用改编自 this answer 的代码更改 xlsx 文件解决了我的问题

    def repair_broken_excelfile(zipfname, *filenames, new_name=None):
        # https://stackoverflow.com/a/4653863/1562285
        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

    【讨论】:

      【解决方案4】:

      遇到同样的问题,我将文件保存为 xml 格式:“另存为类型:XML Spreadsheet 2003”在窗口中。然后我打开文件并保存为 xlsx 格式。新文件不再给出错误信息。

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          【解决方案6】:

          有时这可以通过在 Excel 中删除表格下方的(空白)行来解决。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2018-09-15
            • 2012-05-16
            • 2021-05-27
            • 2015-05-09
            • 2019-11-05
            • 2021-07-21
            • 2020-04-18
            相关资源
            最近更新 更多