如何将 csv 文件从 google 云端硬盘上传(并使用)到 google colaboratory

问题描述 投票:0回答:5

想尝试Python,而谷歌合作实验室似乎是最简单的选择。我的谷歌驱动器中有一些文件,想将它们上传到谷歌合作实验室。 所以这是我正在使用的代码:

!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中导入并读取shelve或Numpy文件?

将本地数据文件加载到Colaboratory

python google-api google-drive-api google-api-python-client google-colaboratory
5个回答
16
投票
要将 csv 文件从我的 google 驱动器读取到 colaboratory,我需要执行以下步骤:

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

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

3)然后我运行了 download.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 单元中即可连接!

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


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

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


0
投票
enter code here
My Way(未经授权)

在 csv 文件链接中查找文件 ID(文件公共)->

https://drive.google.com/file/d/***ID***/view?usp=sharing

例如->

https://drive.google.com/file/d/***1MeA00way9lA3_lllI-lsCefPxxxXXXXXX***/view? USP=分享

输入您的 ID -> url =“https://drive.google.com/uc?export=download&id=

ID

例如 url =“https://drive.google.com/uc?export=download&id=

1MeA00way0lA3_lllI-lsCefPxxxXXXXX

最终代码

url =“https://drive.google.com/uc?export=download&id=1MeA99way2lA3_mlyI-lsCefPubyXZ9cO”

df = pd.read_csv(url)

© www.soinside.com 2019 - 2024. All rights reserved.