【问题标题】:Pandas and Spreadsheet熊猫和电子表格
【发布时间】:2018-06-09 14:08:38
【问题描述】:

我正在使用 ubuntu,因此无法使用 ms excel。无论如何我已经创建了一个电子表格,我希望在我的 python 程序中使用它。 以下是 Python 程序。

import pandas as pd

df=pd.read_excel("/home/files/file1.ods")

df.head()

Traceback (most recent call last):
File "spreadsheet.py", line 2, in <module>
  df=pd.read_excel("/home/files/file1.ods")
File "/usr/lib/python3/dist-packages/pandas/io/excel.py", line 163, in    read_excel
  io = ExcelFile(io, engine=engine)
File "/usr/lib/python3/dist-packages/pandas/io/excel.py", line 187, in __init__
  import xlrd  # throw an ImportError if we need to
ImportError: No module named 'xlrd'

这是否意味着我必须使用 ms excel 或者我的理解有误。无论如何,您的帮助将不胜感激。

【问题讨论】:

  • 你试过 pip install xlrd 吗?
  • 如果您使用的是 pycharm 之类的东西,请将 xlrd 添加到您的项目/环境中
  • 安装 xlrd 是不够的。该函数名为 read_excel,用于读取 excel 文件(xls 和 xlsx)。想看ods的可以check here。更好的是,只需使用 csv 和 read_csv 函数。您可以使用 libreoffice 保存到 csv、xls 或 xlsx。
  • read_csv 工作!!谢谢。

标签: python python-3.x pandas


【解决方案1】:

在 Pandas 的最新版本(自 0.25 起)中,提供了此功能。

只需安装odfpy 包(使用pip install odfpy 或等),然后使用pandas 的(可悲的)read_excel() 函数和engine='odf' 选项。例如:

pd.read_excel('path_to_file.ods', engine='odf')

https://pandas.pydata.org/pandas-docs/stable/user_guide/io.html#opendocument-spreadsheets

【讨论】:

    猜你喜欢
    • 2014-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-20
    • 2017-11-12
    • 2017-10-04
    • 2022-11-10
    • 2020-02-27
    相关资源
    最近更新 更多