简体   繁体   English

通过 gspread 从本地安装的驱动器中获取 Google 表格数据

[英]Fetching Google Sheet data from locally mounted drive via gspread

I am working on a Google Colab notebook that requires the user to mount google drive using the colab.drive python library.我正在开发一个谷歌 Colab 笔记本,它要求用户使用colab.drive python 库安装谷歌驱动器。 They then input relative paths on the local directory tree ( /content/drive/... by default on that mount) to files of interest for analysis.然后,他们将本地目录树上的相对路径( /content/drive/...默认情况下在该挂载上)输入到感兴趣的文件以进行分析。 Now, I want to use a Google Sheet they can create as a configuration file.现在,我想使用他们可以创建为配置文件的 Google 表格。 There is lots of info on how to authenticate gspread and fetch a sheet from its HTTPS url , but I can't find any info on how to access the .gsheet file using gspread that is already mounted on the local filesystem of the colab runtime.有很多关于如何验证gspread从其 HTTPS url获取工作表的信息,但我找不到任何有关如何使用已安装在 colab 运行时的本地文件系统gspread的 gspread 访问.gsheet文件的信息。

There are many tutorials using this flow: https://colab.research.google.com/notebooks/io.ipynb#scrollTo=yjrZQUrt6kKj , but I don't want to make the user authenticate twice (having already done so for the initial mount), and i don't want to make them input some files as relative path, some as HTTPS URL.有许多使用此流程的教程: https://colab.research.google.com/notebooks/io.ipynb#scrollTo=yjrZQUrt6kKj ,但我不想让用户进行两次身份验证(已经这样做了初始mount),我不想让他们输入一些文件作为相对路径,一些作为 HTTPS URL。

I had thought this would be quite like using gspread to work with google sheets that I might have on my locally mounted drive as well.我原以为这很像使用gspread来处理我本地安装的驱动器上可能有的谷歌表格。 But, I haven't seen this workflow anywhere either.但是,我也没有在任何地方看到过这个工作流程。 Any pointers in that direction might help me out as well.朝那个方向的任何指示也可能对我有所帮助。

Thank you!谢谢!

Yes, we can import data from Google Sheets too, using the existing open-source gspread library for interacting with Sheets.是的,我们也可以从 Google 表格导入数据,使用现有的开源 gspread 库与表格进行交互。 But First, we need to install the package using pip.但首先,我们需要使用 pip 安装 package。

!pip install — upgrade -q gspread

Next, we'll import the library, authenticate, and create the interface to sheets.接下来,我们将导入库、进行身份验证并创建工作表接口。

from google.colab import auth
auth.authenticate_user()
import gspread
from oauth2client.client import GoogleCredentials
gc = gspread.authorize(GoogleCredentials.get_application_default())

After executing this code, you will find a input box same as we saw while getting permission for google drive.执行此代码后,您会发现一个与我们在获得 google drive 权限时看到的相同的输入框。 Visit link and login in your google account and copy and paste the verification code in this box.访问链接并登录您的谷歌帐户,然后将验证码复制并粘贴到此框中。

Now, you can upload as well as download data to and from google sheets.现在,您可以向谷歌表格上传和下载数据。 To read data from google sheet execute the following code.要从 google sheet 中读取数据,请执行以下代码。

# Open your exisiting sheet and read some data.
worksheet = gc.open(‘spreadsheet name’).sheet1
# get_all_values gives a list of rows.
rows = worksheet.get_all_values()
print(rows)
# Convert to a DataFrame and render.
import pandas as pd
pd.DataFrame.from_records(rows)

Instead of adding .gsheet on colab's drive you can try storing it in the user's drive and later fetch from there when needed.您可以尝试将其存储在用户的驱动器中,然后在需要时从那里获取,而不是在 colab 的驱动器上添加.gsheet So until that kernel is running you won't have to re-authenticate the user.因此,在 kernel 运行之前,您无需重新验证用户身份。

I'm also not finding anything to authenticate into colab from other device.我也没有找到任何可以从其他设备验证到 colab 的内容。 So you would consider modifying your flow a bit.所以你会考虑稍微修改一下你的流程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM