首页 文章

如何从谷歌驱动器上传csv文件(并使用它)到google colaboratory

提问于
浏览
11

想尝试python,谷歌colaboratory似乎是最简单的选择 . 我有一些文件在我的谷歌驱动器,并希望上传到谷歌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. 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')

基本上,对于用户“abc”,我想从文件夹“def”上传文件xyz.csv . 我可以上传文件,但是当我要求 Headers 时, Headers 是“无 Headers ” . 当我要求上传的文件的ID时,它每次都会更改,因此我无法使用Id .

我怎么读文件???并设置一个正确的文件名???

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

这是我发现的一些其他链接..

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

Load local data files to Colaboratory

3 回答

  • 0

    要从我的google驱动器中读取csv文件到colaboratory,我需要执行以下步骤:

    1)我首先需要授权colaboratory使用PyDrive访问我的谷歌硬盘 . 我用它们的代码示例 . (粘贴在下面)

    2)我还需要登录我的drive.google.com以查找我想下载的文件的目标ID . 我通过右键单击文件并复制ID的共享链接找到了这个 . id看起来像这样:'1BH-rffqv_1auzO7tdubfaOwXzf278vJK'

    3)然后我运行了downloads.GetContentFile('myName.csv') - 输入我想要的名字(在你的情况下是xyz.csv)

    这似乎对我有用!

    我使用了他们在示例中提供的代码:

    # 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')
    
  • 0

    文件创建将文件正文作为其第一个参数 . 如果您查看file create的文档,则可以填写许多字段 . 在下面的示例中,您将它们添加到以逗号分隔的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()
    

    我建议你阅读文档的file upload部分,以更好地了解上传的工作原理以及哪些文件实际上可以从谷歌驱动器中读取 . 我不确定这是否会让您访问Google colaborate

    可能修复您的代码 .

    我不是一个python dev,但我的猜测是你可以通过这样做来设置你的头衔 .

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

    好吧,我很确定我已经很晚了,但我想把它放在那里,以防万一 . 我认为你能做到这一点的最简单方法是

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

    这将生成一个链接,单击它并使用Google OAuth登录,将密钥粘贴到colab单元格中并且您已连接!

    查看左侧边栏中的可用文件列表,然后复制要访问的文件的路径 . 像其他任何文件一样阅读它 .

相关问题