How to download a csv file (and use it) from Google Drive to Google colaboratory

I wanted to try python, and goabor colaboratory seemed the easiest option. I have files on my Google drive and you want to upload them to Google colaboratory. so here is the code i use:

!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. Create & upload a file text file. uploaded = drive.CreateFile({'xyz.csv': 'C:/Users/abc/Google Drive/def/xyz.csv'}) uploaded.Upload() print('Uploaded file with title {}'.format(uploaded.get('title'))) import pandas as pd xyz = pd.read_csv('Untitled.csv') 

Basically, for the user "abc", I wanted to download the xyz.csv file from the "def" folder. I can upload the file, but when I ask for the name, it says the name is "Untitled". when I request the id of the downloaded file, it changes every time, so I can not use the id.

How do I read a file ??? and set the correct file name ???

 xyz = pd.read_csv('Untitled.csv') doesnt work xyz = pd.read_csv('Untitled') doesnt work xyz = pd.read_csv('xyz.csv') doesnt work 

Here are some other links I found ..

How to import and read a Numpy shelf or file in Google Colaboratory?

Upload local data files to the Colaboratory

+5
source share
2 answers

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

1) First I needed to enable collaboration to access my Google drive with PyDrive. I used a sample code for this. (inserted below)

2) I also needed to log in to my .google.com drive to find the target identifier 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 identifier looks something like this: '1BH-rffqv_1auzO7tdubfaOwXzf278vJK'

3) Then I ran download.GetContentFile ('myName.csv') - adding the name I wanted (in your case, it is xyz.csv)

It seems to work for me!

I used the code that 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') 
+3
source

Creating a file takes the body of the file; I am its first parameter. If you check the documentation for file create , you can fill out several fields. In the example below, you would add them to comma-separated file_metadata.

 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 in order to better understand how the upload works and what files you can actually read from in the Google driver. I'm not sure if this will give you access to google colaborate

Possible fix for your code.

I'm not a python developer, but I think you can set your title by doing this.

 uploaded = drive.CreateFile({'xyz.csv': 'C:/Users/abc/Google Drive/def/xyz.csv', 'name': 'xyz.csv'}) 
0
source

Source: https://habr.com/ru/post/1274944/


All Articles