简体   繁体   English

如何在列中搜索文本,然后使用该列使用 gspread 和 python 打印整行?

[英]How do i search for a text in a column and then use that column to print the entire row using gspread & python?

Here's the example of my code.这是我的代码示例。

cell_list = wks.find(input_ID)
print("Text found at R%sC%s" % (cell_list.row, cell_list.col))
get_row = int(("%s" % (cell_list.row)))
print(get_row)
searched_row = wks.row_value(get_row)
print(searched_row)

The error I'm getting...我得到的错误...

Text found at R5C5
5
Traceback (most recent call last):
  File "d:/Users/Win81/Desktop/testing.py", line 18, in <module>
    searched_row = wks.row_value(get_row)
AttributeError: 'Worksheet' object has no attribute 'row_value'

I realize the problem is at the get_row.我意识到问题出在 get_row 上。 I've searched up online and all the examples are putting in a number in the bracket.我在网上搜索过,所有的例子都在括号里放了一个数字。 However, how do I go about doing this?但是,我该怎么做呢? Thank you for your help!感谢您的帮助!

You have a typo in the line where you are trying to get the row values:您在尝试获取行值的行中有一个错字:

  • Currently: searched_row = wks.row_value(get_row)当前: searched_row = wks.row_value(get_row)
  • Should be: searched_row = wks.row_values(get_row)应该是: searched_row = wks.row_values(get_row)

(Notice plural row_values ... not singular row_value ) (注意复数row_values ...不是单数row_value

When I make this correction, your code runs fine on an example that I set up to replicate your problem:当我进行此更正时,您的代码在我为复制您的问题而设置的示例上运行良好:

(BEFORE THE TYPO FIX)
> python gspread-fix.py
Text found at R5C5
5
Traceback (most recent call last):
  File "gspread-fix.py", line 20, in <module>
    searched_row = wks.row_value(get_row)
AttributeError: 'Worksheet' object has no attribute 'row_value'

(AFTER THE TYPO FIX)
>python gspread-fix.py
Text found at R5C5
5
['y', 'z', '0', '1', '2', '3']

For reference, here's a copy of the complete code with the typo fix and a snapshot of the Google Sheet that I used to test and verify the fix:作为参考,这里有一份完整代码的副本,其中包含打字错误修复以及我用来测试和验证修复的 Google Sheet 的快照:

import gspread

gc = gspread.service_account()

# Open a sheet from a spreadsheet in one go
wks = gc.open("gspread-test").sheet1

input_ID = '2'
# original
cell_list = wks.find(input_ID)
print("Text found at R%sC%s" % (cell_list.row, cell_list.col))
get_row = int(("%s" % (cell_list.row)))
print(get_row)
searched_row = wks.row_values(get_row)
print(searched_row)

在此处输入图片说明

For further reference, if others are interested in getting started with gspread, I found this quick authentication run-through very helpful:作为进一步参考,如果其他人对 gspread 入门感兴趣,我发现这个快速身份验证运行非常有帮助:

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

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