13

想尝试 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

这是我找到的其他一些链接..

如何在 Google Colaboratory 中导入和读取搁置文件或 Numpy 文件?

将本地数据文件加载到 Colaboratory

4

4 回答 4

16

要将我的谷歌驱动器中的 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') 
于 2018-02-01T20:09:04.593 回答
3

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

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

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

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

于 2018-10-01T15:00:11.003 回答
1

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

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

可能修复您的代码。

我不是 python 开发者,但我猜你可以通过这样做来设置你的标题。

uploaded = drive.CreateFile({'xyz.csv': 'C:/Users/abc/Google Drive/def/xyz.csv',
                             'name': 'xyz.csv'})
于 2018-01-25T07:29:15.680 回答
0

我认为这个命令就这么简单

# 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 类似。

于 2020-04-22T13:18:02.267 回答