Python Jupyter Notebook - 无法通过路径打开 CSV 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43298291/
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
Python Jupyter Notebook - Unable to open CSV file through a path
提问by XxEthan70xX
I am a little new to Python, and I have been using the Jupyter Notebook through Anaconda. I am trying to import a csv file to make a DataFrame, but I am unable to import the file.
我对 Python 有点陌生,我一直在通过 Anaconda 使用 Jupyter Notebook。我正在尝试导入一个 csv 文件来制作一个 DataFrame,但我无法导入该文件。
Here is an attempt using the local method:
这是使用本地方法的尝试:
df = pd.read_csv('Workbook1')
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-11-a2deb4e316ab> in <module>()
----> 1 df = pd.read_csv('Workbook1')
After that I tried using the path (I put user for my username)
之后我尝试使用路径(我把用户作为我的用户名)
df = pd.read_csv('Users/user/Desktop/Workbook1.csv')
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-13-3f2bedd6c4de> in <module>()
----> 1 df = pd.read_csv('Users/user/Desktop/Workbook1.csv')
I am using a Mac, which I am also new to, and I am not 100% sure if I am correctly importing the right path. Can anyone offer some insight or solutions that would allow me to open this csv file.
我正在使用 Mac,我也是新手,我不能 100% 确定我是否正确导入了正确的路径。任何人都可以提供一些见解或解决方案,让我打开这个 csv 文件。
回答by RATAN KUMAR
Instead of providing path, you can set a path using the code below:
您可以使用以下代码设置路径,而不是提供路径:
import os
import pandas as pd
os.chdir("D:/dataset")
data = pd.read_csv("workbook1.csv")
This will surely work.
这肯定会奏效。
回答by mgig
Are you sure that the file exists in the location you are specifying to the pandas read_csv
method? You can check using the os
python built in module:
您确定该文件存在于您为 pandasread_csv
方法指定的位置吗?您可以使用os
python 内置模块进行检查:
import os
os.path.isfile('/Users/user/Desktop/Workbook1.csv')
Another way of checking if the file of interest is in the current working directory within a Jupyter notebook is by running ls -l
within a cell:
检查感兴趣的文件是否在 Jupyter 笔记本中的当前工作目录中的另一种方法是在ls -l
单元格中运行:
ls -l
回答by Sham Pat
I think the problem is probably in the location of the file:
我认为问题可能出在文件的位置:
df1 = pd.read_csv('C:/Users/owner/Desktop/contacts.csv')
Having done that, now you can play around with the big file if you have, and create useful data with:
完成后,现在您可以使用大文件(如果有),并使用以下方法创建有用的数据:
df1.head()
回答by soumya gupta
The OS module in python provides functions for interacting with the operating system. OS, comes under Python's standard utility modules.
python中的OS模块提供了与操作系统交互的功能。OS,属于 Python 的标准实用程序模块。
import os
import pandas as pd
os.chdir("c:\Pandas")
df=pd.read_csv("names.csv")
df
This might help. :)
这可能会有所帮助。:)
回答by user11699304
The file name is case sensitive, so check your case.
文件名区分大小写,因此请检查您的大小写。
回答by user12105229
I had the same problem on a Mac too and for some reason it only happened to me there. And I tried to use many tricks but nothing works. I recommend you go directly to the file, right click and then press “alt” key after that the option to “copy route” will appear, and just paste it into your jupyter. For some reason that worked to me.
我在 Mac 上也遇到了同样的问题,出于某种原因,它只发生在我身上。我尝试使用很多技巧,但没有任何效果。我建议您直接转到该文件,右键单击然后按“alt”键,然后将出现“复制路由”选项,然后将其粘贴到您的 jupyter 中。出于某种原因,这对我有用。
回答by schmüdde
I believe the issue is that you're not using fully qualified paths. Try this:
我认为问题在于您没有使用完全限定的路径。尝试这个:
Move the data into a suitable project directory. You can do this using the %%bash
Magic commands.
将数据移动到合适的项目目录中。您可以使用%%bash
Magic 命令执行此操作。
%%bash
mkdir -p /project/data/
cp data.csv /project/data/data.csv
You can read the file
你可以读取文件
f = open("/project/data/data.csv","r")
print(f.read())
f.close()
But it might be most useful to load it into a library.
但将其加载到库中可能最有用。
import pandas as pd
data = pd.read_csv("/project/data/data.csv")
I've created a runnable Jupyter notebook with more details here: Jupyter Basics: Reading Files.
我在此处创建了一个可运行的 Jupyter 笔记本,其中包含更多详细信息:Jupyter Basics: Reading Files。
回答by Shri
Try double quotes, instead of single quotes. it worked for me.
尝试双引号,而不是单引号。它对我有用。