如何将csv文件上传到Google驱动器并将其从中读取到python中 [英] How to upload csv file into google drive and read it from same into python

查看:60
本文介绍了如何将csv文件上传到Google驱动器并将其从中读取到python中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个已经上传了我的csv文件的google驱动器,共享该文件的链接为:

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:/用户/.../Google云端硬盘/

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

请给我分步指南,以实现如何直接从Google驱动器读取此特定的csv文件,而不是先将其下载到我的PC上,然后再读取为python.

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:

它对我不起作用,导致出现以下错误:

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'

推荐答案

这是我用于存储在Google云端硬盘中的所有csv文件的一种简单方法.

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

下一步是身份验证,并创建PyDrive客户端以连接到您的云端硬盘.

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

这应该为您提供一个连接到Google Cloud SDK的链接.

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

选择要访问的Google云端硬盘帐户.复制链接并将其粘贴到Colab Notebook的文本字段提示中.

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)

要获取文件,您将需要Google云端硬盘中的文件ID.

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

最后,您可以将文件读取为pandas数据框.

Finally, you can read the file as pandas dataframe.

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

这篇关于如何将csv文件上传到Google驱动器并将其从中读取到python中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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