gpt4 book ai didi

Python gspread CellNotFound 异常错误

转载 作者:太空宇宙 更新时间:2023-11-03 19:57:53 24 4
gpt4 key购买 nike

我想检查我的工作表中是否存在单元格值。如果单元格存在,则返回 yes,如果不存在,则返回 no。

cell = sheet.find('test')
if cell.value =='test':
print('Yes')
else:
print('No')

上面的代码适用于 true 段,但如果 false 则不起作用,并返回错误 gspread.exceptions.CellNotFound: test。我尝试添加异常 except gspread.exceptions.CellNotFound: 但它仍然不起作用。

这段代码有什么问题吗?

最佳答案

  • 您想要使用文本搜索电子表格中工作表上的单元格。
  • 您希望使用 gspread 和 python 来实现此目的。
  • 您已经能够使用 Sheets API 获取和输入电子表格的值。

如果我的理解是正确的,这个答案怎么样?请将此视为几个可能答案之一。

模式 1:

何时 cell = sheet.find('test')使用时 test在工作表中找不到,错误为gspread.exceptions.CellNotFound: test发生。虽然我无法理解I tried adding an exception, except gspread.exceptions.CellNotFound: but it still does not work. ,我认为你的做法是正确的。那么你可以测试下面的脚本吗?

示例脚本:

try:
cell = sheet.find('test')
print('Yes')
except gspread.exceptions.CellNotFound: # or except gspread.CellNotFound:
print('No')

模式 2:

何时 cells = sheet.findall('test')使用时 test在工作表中找不到,则返回空数组。在这个模式中,使用了这个。

示例脚本:

cells = sheet.findall('test')
if len(cells) > 0: # or if cells != []:
print('Yes')
else:
print('No')

注意:

  • 如果上述脚本不起作用,您可以发布整个脚本吗?借此,我想确认一下。当然,请删除您的个人信息。

引用文献:

如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

关于Python gspread CellNotFound 异常错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59446405/

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