无法将 .xlsx 导入 Python:没有这样的文件或目录 [英] Unable to import .xlsx into Python: No such file or directory

查看:29
本文介绍了无法将 .xlsx 导入 Python:没有这样的文件或目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据从 HW3_Yld_Data.xlsx 导入 Python.我确保 Excel 文件与 Python 文件位于同一目录中.这是我写的:

I'm trying to import data from HW3_Yld_Data.xlsx into Python. I made sure that the Excel file is in the same directory as the Python file. Here's what I wrote:

import pandas as pd

Z = pd.read_excel('HW3_Yld_Data.xlsx')

这是我得到的错误:

In [2]: import pandas as pd
   ...: 
   ...: Z = pd.read_excel('HW3_Yld_Data.xlsx')
Traceback (most recent call last):

  File "<ipython-input-2-7237c05c79ba>", line 3, in <module>
    Z = pd.read_excel('HW3_Yld_Data.xlsx')

  File "/Users/Zhengnan/anaconda/lib/python2.7/site-packages/pandas/io/excel.py", line 151, in read_excel
return ExcelFile(io, engine=engine).parse(sheetname=sheetname, **kwds)

  File "/Users/Zhengnan/anaconda/lib/python2.7/site-packages/pandas/io/excel.py", line 188, in __init__
self.book = xlrd.open_workbook(io)

  File "/Users/Zhengnan/anaconda/lib/python2.7/site-packages/xlrd/__init__.py", line 394, in open_workbook
f = open(filename, "rb")

IOError: [Errno 2] No such file or directory: 'HW3_Yld_Data.xlsx'

令人难以置信的是,它曾经运行良好.在我昨天执行了conda update --all"后,它似乎停止工作了.

What's mind-boggling is that it used to work fine. It appeared to stop working after I did a "conda update --all" yesterday.

顺便说一句,我使用 Spyder 作为 IDE.请帮忙.谢谢.

BTW I'm using Spyder as IDE. Please help. Thank you.

推荐答案

操作系统中的每个进程都有一个当前工作目录.任何相对路径都是相对于当前工作目录的.

Each process in the operating system has a current working directory. Any relative path is relative to the current working directory.

当前工作目录设置为您启动进程的目录.这在使用命令行时很自然,但对于只使用 GUI 的人来说会令人困惑.

The current working directory is set to the directory from which you launched the process. This is very natural when using the command-line, but get be confusing for people only using GUIs.

您可以使用 os.getcwd()<检索它/code>,您可以使用 更改它os.chdir().当然,您也可以在启动脚本之前对其进行更改.

You can retrieve it using os.getcwd(), and you can change it using os.chdir(). Of course, you can also change it before launching your script.

这篇关于无法将 .xlsx 导入 Python:没有这样的文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆