how to read data from google drive using Colaboratory (google)

懵懂的女人 提交于 2020-12-06 04:43:33

问题


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.

QUESTION

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')

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


回答1:


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')

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')


来源:https://stackoverflow.com/questions/48596521/how-to-read-data-from-google-drive-using-colaboratory-google

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!