如何使用Colaboratory(google)从Google驱动器读取数据 [英] how to read data from google drive using Colaboratory (google)

查看:119
本文介绍了如何使用Colaboratory(google)从Google驱动器读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Colaboratory的新手,并且想要设置一个存储在我的Google驱动器中的小项目.在我的Google驱动器上,我创建了一个文件夹"TheProject",在其中创建了两个文件夹:代码"和数据".在文件夹代码"中,我创建了一个新的colab笔记本,并且在数据"文件夹中有多个数据集.

I'm new to Colaboratory and would like setup a small project which is stored on my google drive. On my google drive, I create a folder 'TheProject', where I created two folders: 'code' and 'data'. I the folder 'code' I created a new colab notebook and I have several data sets in 'data' folder.

问题

如何从Google驱动器上的文件夹中将数据读取到colab笔记本中? 例如:

How to read data into colab notebook from a folder on the google drive? For example:

data = pd.read_excel('SOME_PATH/TheProject/data/my_data.xlsx')

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

where SOME_PATH should indicate how to get to the main folder 'TheProject' and read data from 'data' folder.

推荐答案

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

Right click on your file on Google Drive and get its sharable link. from that link you'll extract the file 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')

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

Once you hit these commands you will be prompted a link that asks you to grant permission to Google Drive. It'll give you a token that you must type in a text box.

现在您可以加载文件了:

Now you're ready to load your file:

data = pd.read_csv('file.csv')

这篇关于如何使用Colaboratory(google)从Google驱动器读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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