简体   繁体   English

如何使用 jupyter notebook 在桌面上读取文件?

[英]How do you read files on desktop with jupyter notebook?

I launched Jupyter Notebook , created a new notebook in python , imported the necessary libraries and tried to access a .xlsx file on the desktop with this code :我启动了Jupyter Notebook ,在python中创建了一个新笔记本,导入了必要的libraries并尝试使用以下code访问桌面上的.xlsx文件:

haber = pd.read_csv('filename.xlsx')

but error keeps popping up.但错误不断弹出。 Want a reliable way of accessing this file on my desktop without incurring any error response想要一种可靠的方式在我的桌面上访问此文件而不会产生任何错误响应

This is an obvious path problem, because your notebook is not booted on the desktop path, you must indicate the absolute path to the desktop file, or the relative path relative to the jupyter boot directory.这是一个明显的路径问题,因为你的notebook不是在桌面路径上启动的,所以必须指明桌面文件的绝对路径,或者相对于jupyter启动目录的相对路径。

If you are using linux如果您使用的是 linux

/home/(your user name)/Desktop/(your filename)

if you are on windows如果您在 windows

C:\Users\(your user name)\Desktop\( your filename)

and if your python file is on same path where dataset file is then just give the file name with extension如果您的 python 文件位于数据集文件所在的同一路径上,则只需提供带有扩展名的文件名

You will need to enter the full path of your excel file.您需要输入 excel 文件的完整路径。

First:第一的:

Open your excel file, right click on the file and click on "Copy path to clipboard".打开您的 excel 文件,右键单击该文件并单击“复制路径到剪贴板”。

Second:第二:

Next paste your path in your script.接下来将路径粘贴到脚本中。 Mine looks something like this:我的看起来像这样:

#only using one backslash "\"
'C:\Users\...YourFileName.xlsx'

Third:第三:

You will likely have to modify this path by adding two "\" instead of one "\" in each spot you only see one backslash.您可能必须通过在每个位置添加两个“\”而不是一个“\”来修改此路径,您只会看到一个反斜杠。

For example, my new path would now look like this:例如,我的新路径现在看起来像这样:

#using two backslashes now "\\"
'C:\\Users\\...YourFileName.xlsx'

An example of your final output will look like this:您的最终 output 示例如下所示:

haber = pd.read_csv('C:\\Users\\...YourFileName.xlsx')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM