Pandas.read_excel:访问主目录 [英] Pandas.read_excel: Accessing the home directory
问题描述
[找到解决方案]
尝试使用pandas.read_excel
访问我的主目录时,我遇到了一些意外的行为.
I have encountered some unexpected behavior when trying to access my home directory using pandas.read_excel
.
要访问的文件可以在以下位置找到
The file I want to access can be found at
/users/isys/orsheridanmeth
这是cd ~/
带我去的地方.我要访问的文件是
which is where cd ~/
takes me to. The file I would like to access is
'~/workspace/data/example.xlsx'
以下内容可用于读取excel文件(使用import pandas as pd
):
The following works to read in the excel file (using import pandas as pd
):
df = pd.read_excel('workspace/data/example_.xlsx', 'Sheet1')
而
df = pd.read_excel('~/workspace/data/example.xlsx', 'Sheet1')
给我以下错误:
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_csv 但是在我使用pd.read_csv('~/workspace/data/example.csv')
时有效.
pandas.read_csv however worked when I used pd.read_csv('~/workspace/data/example.csv')
.
我想继续使用文件的相对路径.为什么pandas.read_excel
不能正常工作?
I would like to continue to use this relative paths to files. Any explanation why this doesn't work with pandas.read_excel
?
使用xlrd
Using xlrd
使用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'
[解决方案]
from os.path import expanduser as ospath
df = pd.read_excel(ospath('~/workspace/data/example.xlsx'), 'Sheet1')
推荐答案
我相信~
由shell扩展-在这种情况下,您的代码实际上是在尝试打开以~
开头的路径.奇怪的是,这行不通. :-)
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. :-)
首先尝试通过os.path.expanduser()
运行路径-应该可以将~
变量扩展为实际值.
Try running the path through os.path.expanduser()
first - that should work to expand the ~
variable to the real value.
您可能还想研究os.path.expandvars()
.
希望有帮助
这篇关于Pandas.read_excel:访问主目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!