Python gspread CellNotFound异常

2024-10-01 15:30:29 发布

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

我想检查我的工作表中是否存在单元格值。如果单元格存在,则返回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:,但仍然不起作用。你知道吗

有没有线索表明这个代码有什么问题?你知道吗


Tags: no代码testifvaluecellfindelse
1条回答
网友
1楼 · 发布于 2024-10-01 15:30:29
  • 您希望使用文本搜索电子表格中工作表上的单元格。你知道吗
  • 您希望使用gspread和python来实现这一点。你知道吗
  • 您已经能够使用sheetsapi获取和放置电子表格的值。你知道吗

如果我的理解是正确的,这个答案怎么样?请把这看作是几种可能的答案之一。你知道吗

模式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')

注:

  • 如果上面的脚本不起作用,你能发布你的整个脚本吗?我想确认一下。当然,请删除您的个人信息。你知道吗

参考文献:

如果我误解了你的问题,而这不是你想要的方向,我道歉。你知道吗

相关问题 更多 >

    热门问题