英文:
File location macro for python (Jupyter notebook)
问题
我是Python/Jupyter笔记本新手。有没有办法将文件位置保存为宏,以便我在每次打开来自同一文件夹的数据集时不必复制和粘贴相同的位置?
dat = pd.read_csv("C:/Users/Box Sync/real_world_validation/Data/mydata.csv")
我尝试了以下代码,但没有成功:
data = "C:/Users/Box Sync/real_world_validation/Data"
dat = pd.read_csv("data/mydata.csv")
英文:
I am new to python/Jupyter notebook. Is there a way I can save the file location as a macro so that I don't need to copy and paste the same location each time I open a dataset from the same folder?
dat = pd.read_csv("C:/Users/Box Sync/real_world_validation/Data/mydata.csv")
I tried the following code, but did not work:
data = "C:/Users/Box Sync/real_world_validation/Data"
dat = pd.read_csv("data/mydata.csv")
答案1
得分: 0
import os
os.chdir(r"C:/Users/Box Sync/real_world_validation/Data")
dat = pd.read_csv("mydata.csv")
英文:
Or if your notebook will always be working in 1 directory you could change the current working directory altogether
import os
os.chdir(r"C:/Users/Box Sync/real_world_validation/Data")
dat = pd.read_csv("mydata.csv")
答案2
得分: 0
Sure, here's the translation of the code part:
要访问位于相同文件夹中的数据,我在这里发布 @abhilb 的答案:
path = "C:/Users/Box Sync/real_world_validation/Data"
data = pd.read_csv(path + "/mydata.csv")
英文:
To get access to the data in the same folder, I post the answer by @abhilb here:
path = "C:/Users/Box Sync/real_world_validation/Data"
data = pd.read_csv(path + "/mydata.csv")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论