Pandas.read_excel:访问主目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38476733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:37:44  来源:igfitidea点击:

Pandas.read_excel: Accessing the home directory

pythonpandaspath

提问by oliversm

[Solution Found]

[找到解决方案]

I have encountered some unexpected behavior when trying to access my home directory using pandas.read_excel.

尝试使用pandas.read_excel.

The file I want to access can be found at

我要访问的文件可以在

/users/isys/orsheridanmeth

which is where cd ~/takes me to. The file I would like to access is

这是cd ~/带我去的地方。我想访问的文件是

'~/workspace/data/example.xlsx'

The following works to read in the excel file (using import pandas as pd):

以下工作在 excel 文件中读取(使用import pandas as pd):

df = pd.read_excel('workspace/data/example_.xlsx', 'Sheet1')

whereas

然而

df = pd.read_excel('~/workspace/data/example.xlsx', 'Sheet1')

gives me the following error:

给了我以下错误:

df = pd.read_excel('~/workspace/data/example.xlsx', 'Sheet1')
Traceback (most recent call last):
  File "/users/is/ahlpypi/egg_cache/i/ipython-3.2.0_1_ahl1-py2.7.egg/IPython/core/interactiveshell.py", line 3035, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-397-4412a9e7c128>", line 1, in <module>
    df = pd.read_excel('~/workspace/data/example.xlsx', 'Sheet1')
  File "/users/is/ahlpypi/egg_cache/p/pandas-0.16.2_ahl1-py2.7-linux-x86_64.egg/pandas/io/excel.py", line 151, in read_excel
    return ExcelFile(io, engine=engine).parse(sheetname=sheetname, **kwds)
  File "/users/is/ahlpypi/egg_cache/p/pandas-0.16.2_ahl1-py2.7-linux-x86_64.egg/pandas/io/excel.py", line 188, in __init__
    self.book = xlrd.open_workbook(io)
  File "/users/is/ahlpypi/egg_cache/x/xlrd-0.9.2-py2.7.egg/xlrd/__init__.py", line 394, in open_workbook
    f = open(filename, "rb")
IOError: [Errno 2] No such file or directory: '~/workspace/data/example.xlsx'

pandas.read_csvhowever worked when I used pd.read_csv('~/workspace/data/example.csv').

pandas.read_csv但是当我使用pd.read_csv('~/workspace/data/example.csv').

I would like to continue to use this relative paths to files. Any explanation why this doesn't work with pandas.read_excel?

我想继续使用这个文件的相对路径。任何解释为什么这不起作用pandas.read_excel

Using xlrd

使用 xlrd

when using xlrdI get a similar error:

使用时xlrd出现类似错误:

import xlrd
xl = xlrd.open_workbook('~/workspace/data/example.xlsx')
Traceback (most recent call last):
  File "/users/is/ahlpypi/egg_cache/i/ipython-3.2.0_1_ahl1-py2.7.egg/IPython/core/interactiveshell.py", line 3035, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-403-90af31feff4b>", line 1, in <module>
    xl = xlrd.open_workbook('~/workspace/data/example.xlsx')
  File "/users/is/ahlpypi/egg_cache/x/xlrd-0.9.2-py2.7.egg/xlrd/__init__.py", line 394, in open_workbook
    f = open(filename, "rb")
IOError: [Errno 2] No such file or directory: '~/workspace/data/example.xlsx'

[SOLUTION]

[解决方案]

from os.path import expanduser as ospath
df = pd.read_excel(ospath('~/workspace/data/example.xlsx'), 'Sheet1')

回答by Paul Walker

I believe ~is expanded by the shell - in which case your code is literally trying to open a path starting with ~. Oddly enough this doesn't work. :-)

我相信~是由 shell 扩展的 - 在这种情况下,您的代码实际上是在尝试打开以~. 奇怪的是,这不起作用。:-)

Try running the path through os.path.expanduser()first - that should work to expand the ~variable to the real value.

首先尝试运行路径os.path.expanduser()- 这应该可以将~变量扩展为实际值。

You may also want to look into os.path.expandvars().

您可能还想查看os.path.expandvars().

Hope that helps

希望有帮助