gpt4 book ai didi

python-3.x - gspread worksheet.update 错误 - 工作表没有属性 'update'

转载 作者:行者123 更新时间:2023-12-02 18:51:19 33 4
gpt4 key购买 nike

我尝试将数据帧写入 Google Colab 中打开的 Google 表格,但收到错误:

属性错误:“工作表”对象没有属性“更新”

我记录并测试了出现错误的各个部分。

# General Imports
# Example at https://colab.research.google.com/notebooks/io.ipynb
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

# Authenticate and create the PyDrive client.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

#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

import numpy as np
import pandas as pd

# Load the DataFrame
dataframe = [['A', 'B', 'C'], ['1', '2' ,'3'], ['Mary', 'Mai', 'Kat']]
print(dataframe)

# Open the Google Sheet
# This assumes that you have worksheet called "RS Output" with sheet "Data" on your Google Drive,

gc = gspread.authorize(GoogleCredentials.get_application_default())

my_workbook = 'RS Output'
my_sheet = "Data"

worksheet = gc.open(my_workbook).worksheet(my_sheet)

list_of_lists = worksheet.get_all_values()
print(list_of_lists)

# update the Google Sheet with the values from the Dataframe
# per gspread documentation at
# https://gspread.readthedocs.io/en/latest/user-guide.html

worksheet.update([dataframe.columns.values.tolist()] + worksheet.values.tolist())

这是输出:

[['A', 'B', 'C'], ['1', '2', '3'], ['Mary', 'Mai', 'Kat']]
[['Testing'], ['This']]
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-132-e085af26b2ed> in <module>()
21 # https://gspread.readthedocs.io/en/latest/user-guide.html
22
---> 23 worksheet.update([dataframe.columns.values.tolist()] + worksheet.values.tolist())

AttributeError: 'Worksheet' object has no attribute 'update'

我似乎找不到如何将数据框写入 Google 表格的明确示例。

谢谢

最佳答案

我遇到了同样的问题,这是我第一次使用 colab.research.google.com 笔记本。原来默认的 gspread 模块是 3.0 版本

!pip install --upgrade gspread!

更新至3.7版本并修复了缺少.update()的问题。

Found existing installation: gspread 3.0.1
Uninstalling gspread-3.0.1:
Successfully uninstalled gspread-3.0.1
Successfully installed gspread-3.7.0

非常感谢:Shashank Rautela

关于python-3.x - gspread worksheet.update 错误 - 工作表没有属性 'update',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66730666/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com