如何使用Colaboratory(谷歌)从谷歌驱动器读取数据

Arn*_*ein 2 python google-drive-api jupyter-notebook

我是Colaboratory的新手,想要设置一个存储在我的谷歌硬盘上的小项目.在我的谷歌硬盘上,我创建了一个文件夹'TheProject',在那里我创建了两个文件夹:'code'和'data'.我的文件夹'代码'我创建了一个新的colab笔记本,我在'data'文件夹中有几个数据集.

如何从谷歌硬盘上的文件夹中将数据读入colab笔记本?例如:

data = pd.read_excel('SOME_PATH/TheProject/data/my_data.xlsx')
Run Code Online (Sandbox Code Playgroud)

其中SOME_PATH应指示如何进入主文件夹'TheProject'并从'data'文件夹中读取数据.

小智 5

右键单击Google云端硬盘上的文件,即可获得其共享链接.从该链接,您将提取文件ID.

! pip install pydrive
# these classes allow you to request the Google drive API
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive 
from google.colab import auth 
from oauth2client.client import GoogleCredentials

# 1. Authenticate and create the PyDrive client.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
file_id = '<your_file_id>'
downloaded = drive.CreateFile({'id': file_id})
# allows you to temporarily load your file in the notebook VM

# assume the file is called file.csv and it's located at the root of your drive
downloaded.GetContentFile('file.csv')
Run Code Online (Sandbox Code Playgroud)

点击这些命令后,系统会提示您一个要求您授予Google云端硬盘权限的链接.它会给你一个你必须在文本框中输入的标记.

现在您已准备好加载文件:

data = pd.read_csv('file.csv')
Run Code Online (Sandbox Code Playgroud)