How to upload csv file (and use it) from google drive into google colaboratory

对着背影说爱祢 提交于 2019-12-02 20:55:08

To read a csv file from my google drive into colaboratory, I needed to do the following steps:

1) I first needed to authorize colaboratory to access my google drive with PyDrive. I used their code example for that. (pasted below)

2) I also needed to log into my drive.google.com to find the target id of the file i wanted to download. I found this by right clicking on the file and copying the shared link for the ID. The id looks something like this: '1BH-rffqv_1auzO7tdubfaOwXzf278vJK'

3) Then I ran downloaded.GetContentFile('myName.csv') - putting in the name i wanted (in your case it is xyz.csv)

This seems to work for me!

I used the code they provided in their example:

# Code to read csv file into colaboratory:
!pip install -U -q PyDrive
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)

#2. Get the file
downloaded = drive.CreateFile({'id':'1BH-rffqv_1auzO7tdubfaOwXzf278vJK'}) # replace the id with id of file you want to access
downloaded.GetContentFile('xyz.csv')  

#3. Read file as panda dataframe
import pandas as pd
xyz = pd.read_csv('xyz.csv') 

Okay I'm pretty sure I'm quite late, but I'd like to put this out there, just in case. I think the easiest way you could do this is by

from google.colab import drive
drive.mount("/content/drive")

This will generate a link, click on it and sign in using Google OAuth, paste the key in the colab cell and you're connected!

check out the list of available files in the side bar on the left side and copy the path of the file you want to access. Read it as you would, with any other file.

File create takes a file body i its first parameter. If you check the documentation for file create there are a number of fields you can fill out. In the example below you would add them to file_metadata comma separated.

file_metadata = {'name': 'photo.jpg'}
media = MediaFileUpload('files/photo.jpg',
                        mimetype='image/jpeg')
file = drive_service.files().create(body=file_metadata,
                                    media_body=media,
                                    fields='id').execute()

I suggest you read the file upload section of the documentation to get a better idea how upload works and which files can actually be read from within google drive. I am not sure that this is going to give you access to Google colaborate

Possible fix for your code.

I am not a python dev but my guess would be you can set your title by doing this.

uploaded = drive.CreateFile({'xyz.csv': 'C:/Users/abc/Google Drive/def/xyz.csv',
                             'name': 'xyz.csv'})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!