如何将csv文件从google drive上传到google colaboratory

2024-10-01 15:32:22 发布

您现在位置:Python中文网/ 问答频道 /正文

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


Tags: 文件csvfromimportauthreadgoogledrive
3条回答

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

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

2)我还需要登录我的drive.google.com找到我想下载的文件的目标id。我是通过右键单击文件并复制ID的共享链接找到这个的。ID如下所示:“1BH-rffqv_1auzO7tdubfaOwXzf278vJK”

3)然后我运行downloaded.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') 

好吧,我很肯定我迟到了,但我想把这个放在外面,以防万一。 我想你最简单的办法就是

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

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

检查左侧侧栏中可用文件的列表,并复制要访问的文件的路径。像你一样读它,和其他文件一起读。

文件创建采用文件体作为第一个参数。如果您检查文档中的file create,您可以填写许多字段。在下面的示例中,您将它们添加到以逗号分隔的文件元数据中。

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 drive中读取。我不确定这是否能让您访问Google colaborate

可能修复代码。

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

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

相关问题 更多 >

    热门问题