How to upload csv file into google drive and read it from same into python

你。 提交于 2021-01-29 06:25:42

问题


I have a google drive which I have my csv file uploaded in already, the link to share that file is given as:

https://drive.google.com/open?id=1P_UYUsgvGXUhPCKQiZWlEAynKoeldWEi

I also know my the directory to the drive as:

C:/Users/.../Google Drive/

Please give me a step-by-step guide to achieving how to read this particular csv file directly from google drive and not by downloading it to my PC first before reading it to python.

I have searched this forum and tried some given solutions such as:

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

It did not work for me, it resulted to the below error:

      3 from pydrive.auth import GoogleAuth
      4 from pydrive.drive import GoogleDrive
----> 5 from google.colab import auth
      6 from oauth2client.client import GoogleCredentials
      7 

ModuleNotFoundError: No module named 'google.colab'

回答1:


You don't need that much out of that example to upload a file to google drive:

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

# access the drive
gauth = GoogleAuth()
drive = GoogleDrive(gauth)

# the file you want to upload, here simple example
f = drive.CreateFile()
f.SetContentFile('document.txt')

# upload the file
f.Upload()
print('title: %s, mimeType: %s' % (f['title'], f['mimeType']))

# read all files, the newly uploaded file will be there
file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
for file1 in file_list:
  print('title: %s, id: %s' % (file1['title'], file1['id']))

Note: I created an empty file in this example instead of an existing one, you just have to change it to load up the csv file from your local pc where the python file is running on instead.

Kind regards




回答2:


Here is a simple approach I use for all my csv files stored in Google Drive.

First import the necessary libraries that will facilitate your connection.

  !pip install -U -q PyDrive

    from google.colab import auth
    from pydrive.auth import GoogleAuth
    from pydrive.drive import GoogleDrive
    from oauth2client.client import GoogleCredentials

Next step is authentication and creating the PyDrive client in order to connect to your Drive.

This should give you a link to connect to Google Cloud SDK.

Select the Google Drive account you want to access. Copy the link and paste it onto the text field prompt on your Colab Notebook.

auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

To get the file, you will need the id of the file in Google Drive.

downloaded = drive.CreateFile({'id':'1P_UYUsgvGXUhPCKQiZWlEAynKoeldWEi'}) # replace the id with id of the file you want to access
downloaded.GetContentFile('file.csv')  

Finally, you can read the file as pandas dataframe.

import pandas as pd
df= pd.read_csv('fle.csv') 


来源:https://stackoverflow.com/questions/52804699/how-to-upload-csv-file-into-google-drive-and-read-it-from-same-into-python

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