【问题标题】:How to upload csv file (and use it) from google drive into google colaboratory如何将 csv 文件(并使用它)从谷歌驱动器上传到谷歌合作实验室
【发布时间】:2018-07-04 08:28:19
【问题描述】:

想试用 python,而 google colaboratory似乎是最简单的选择。我的 google 驱动器中有一些文件,想将它们上传到 google 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。 我可以上传文件,但是当我询问标题时,它说标题是“无标题”。 当我要求上传文件的 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

【问题讨论】:

  • @Bob Smith..你能帮帮我吗

标签: python google-api google-drive-api google-api-python-client google-colaboratory


【解决方案1】:

文件创建的第一个参数是文件主体。如果您查看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 开发者,但我猜你可以通过这样做来设置你的标题。

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

【讨论】:

  • 当我尝试执行上面的代码时,我确实阅读了与错误相关的内容。你是说我不能上传用于 python 代码的 csv 文件吗?你能帮忙指出我在上面的代码中哪里出错了吗?上面的代码只是上传一个 csv 文件(来自谷歌驱动器)并将其分配给一个变量(在谷歌 colaboratory 中)
  • 这是应该上传文本文件的方式..我已经修改它以从文件夹上传 csv 文件。还需要什么改动,请帮忙!
  • 文件正在上传..但标题没有设置..这是为什么?
  • 在元数据中设置名称如上图。你可以上传任何你想要谷歌驱动器的文件,但谷歌驱动器只能显示某些类型的文件,它只能编辑某些类型的文件。据我所知,这与 Google Collaborate 无关。
  • 我认为我无法理解我的观点......我对谷歌驱动器没有任何问题......我已经使用它作为共享文件的方式已经有一段时间了。 .我已经在谷歌驱动器中有csv文件并在R中使用。我想尝试python,而不安装任何软件,我认为谷歌协作可以帮助我做到这一点。在实现任何 python 代码之前,我必须将我将使用的文件分配给适当的变量..这些文件在谷歌驱动器中,我可以上传到谷歌协作,但无法正确命名它们!让我试试你的方法看看。谢谢!
【解决方案2】:

要将我的谷歌驱动器中的 csv 文件读入 colaboratory,我需要执行以下步骤:

1) 我首先需要授权 colaboratory 使用 PyDrive 访问我的谷歌驱动器。我为此使用了他们的代码示例。 (粘贴在下面)

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

3) 然后我运行了下载的.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') 

【讨论】:

    【解决方案3】:

    好的,我很确定我来晚了,但我想把它放在那里,以防万一。 我认为您可以做到这一点的最简单方法是

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

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

    查看左侧边栏中的可用文件列表并复制您要访问的文件的路径。与任何其他文件一起阅读。

    【讨论】:

      【解决方案4】:

      我觉得这个命令就这么简单

      # Mount Google Drive
      import os
      from google.colab import drive
      
      drive.mount('/content/drive')
      !pwd
      !ls
      
      import pandas as pd
      df = pd.read_csv('Untitled.csv')
      

      它将需要您的 Google OAuth 授权,并创建授权密钥。将钥匙放入 colab 单元中。

      请注意!如果您在 Google Drive 中删除或添加文件,有时 google colab 目录中的文件不会更新或与 google drive 类似。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-04
        • 1970-01-01
        • 2021-06-01
        • 1970-01-01
        • 2018-11-04
        • 2020-03-13
        相关资源
        最近更新 更多