【问题标题】:read xlsx file with sheet named as None. pandas xlrd读取名为 None 的工作表的 xlsx 文件。熊猫 xlrd
【发布时间】:2018-12-16 20:40:24
【问题描述】:

我有一堆名为 None(空字符串)的工作表的 xlsx 文件
当我尝试使用pandas 读取文件时,xlrd 会引发list index out of range 错误。

Traceback (most recent call last):
  File "/usr/local/bin/runxlrd.py", line 332, in main
    ragged_rows=options.ragged_rows,
  File "/Library/Python/2.7/site-packages/xlrd/__init__.py", line 416, in open_workbook
    ragged_rows=ragged_rows,
  File "/Library/Python/2.7/site-packages/xlrd/xlsx.py", line 791, in open_workbook_2007_xml
    x12sheet.process_stream(zflo, heading)
  File "/Library/Python/2.7/site-packages/xlrd/xlsx.py", line 528, in own_process_stream
    self_do_row(elem)
  File "/Library/Python/2.7/site-packages/xlrd/xlsx.py", line 667, in do_row
    value = self.sst[int(tvalue)]
IndexError: list index out of range

我在xrld github 中发现了我认为相关的this 问题。
如果我更改工作表的名称,pandas 会成功读取文件。

我无法共享文件作为示例(隐私问题),当我尝试使用 None 作为工作表名称创建演示文件时,Excel 引发了无效名称错误。

包版本。

pkg_resources.get_distribution("xlrd").version
Out[3]: '1.1.0'
pd.__version__
Out[4]: '0.23.0' 

有没有办法用 pandas 或可以更改工作表名称的脚本(任何语言)读取此文件?

【问题讨论】:

  • 您的意思是您正在尝试使用xlrd 打开 Excel 电子表格吗?其中一张被称为无?我不确定xml文件在哪里使用。
  • 对不起,我的错误,我的意思是xlsx 文件。 Pandas 使用 as xlrd 打开xlsx 文件。

标签: python excel pandas xlrd


【解决方案1】:

这适用于我使用 Python 2.7、pandas 0.23.3 和 xlrd 1.1.0

>>> import xlrd
>>> import pandas
>>> xlrd_workbook = xlrd.open_workbook("test.xlsx")
>>> pandas.read_excel(xlrd_workbook, engine='xlrd')
     A    B    C
0  123  456  789

【讨论】:

  • 你将工作表命名为 None 还是空字符串?
  • 我使用 Excel 本身键入了字符串 None。它不会让我把它留空。
  • 空白时出现错误。我猜它是空白的,因为 xlsx 文件是作为系统报告创建的。
猜你喜欢
  • 2023-04-04
  • 1970-01-01
  • 2014-02-03
  • 2017-03-28
  • 1970-01-01
  • 1970-01-01
  • 2020-04-09
  • 1970-01-01
  • 2019-03-31
相关资源
最近更新 更多