【问题标题】:Multiple search strings with re and gspread带有 re 和 gspread 的多个搜索字符串
【发布时间】:2020-04-25 09:25:33
【问题描述】:

我刚刚开始使用 gspread 并寻找有关搜索工作表的一些建议。我想搜索多个字符串并获取两个字符串都存在的特定行的结果。两个字符串必须匹配一个结果(逻辑与)

搜索字符串的一个示例是搜索 IP 地址和主机名。在工作表中,IP 地址将在单元格 A1 中,主机名将在 B1 中。

我正在使用他们文档中的以下代码示例,并尝试了各种迭代,但运气不佳。

amount_re = re.compile(r'(192.168.0.1|Gi0/0.100)')
cell = worksheet.find(amount_re)

Gspread documentation

这是数据的格式:

192.168.0.1,Gi0/0.100
192.168.0.1,Gi0/0.200
192.168.0.1,Gi0/0.300
192.168.0.2,Gi0/0.100

如您所见,A 列和 B 列中有重复项,因此获得唯一结果的唯一方法是同时搜索两者。例如

192.168.0.1,Gi0/0.100

不过,它需要采用 Gspread 搜索格式。我不能只搜索字符串 '192.168.0.1,Gi0/0.100'

【问题讨论】:

  • 我可以问你关于你的问题吗?根据您的问题,我可以理解用于搜索的字符串值放在单元格“A1:B1”中。我能问一下您要搜索的数据信息吗?不幸的是,我无法从你的问题中理解它。这是因为我的英语水平不好。我必须为此道歉。
  • 我已经更新了这个问题。我可以获得“唯一”结果的唯一方法是搜索两个字符串,因为 A 列和 B 列中有重复项。
  • 感谢您回复并添加更多信息。关于the results of the specific row where both strings exist.,您要检索搜索行的单元格的值吗?或者您想检索搜索行的行号?您要检索哪个?
  • 谢谢。真的,我只是想看看该搜索匹配是否在 Google 表格中。但是能够获取行号和单元格数据也很有用。作为背景,我的脚本现在使用本地 csv 工作。它会审计网络设备,当在设备上完成时,它会在 csv 中添加一个条目。稍后当我重新运行时,它会在 csv if search_str in entry 中进行迭代和搜索,如果存在则跳过该条目。 csv 中的搜索字符串将是例如192.168.0.1,Gi0/0.100 现在我想迁移到工作表和 gspread 并做同样的事情。
  • 感谢您的回复。根据您的回复和问题,我提出了 2 种模式的示例脚本作为答案。你能确认一下吗?如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

标签: python gspread python-re


【解决方案1】:

我相信你的目标如下。

  • 您想从 Google 电子表格的工作表中搜索 2 个值,例如 192.168.0.1Gi0/0.100
  • 这 2 个值对应于“A”和“B”列。
  • 192.168.0.1Gi0/0.100 等2 个值出现在同一行时,您需要检索这些值。
  • 您希望使用 gspread 和 python 来实现此目的。
  • 您已经使用 Sheets API 获取和放置 Google 电子表格的值。

为了实现你的目标,这个答案怎么样?

我认为很遗憾,re.compile(r'(192.168.0.1|Gi0/0.100)') 不能用于实现您的目标。所以在这里,我想提出以下2种模式。

模式一:

在此模式中,使用查询语言搜索值。访问令牌可用于 gspread 的授权。

示例脚本:

searchValues = ["192.168.0.1", "Gi0/0.100"]  # Please set the search values.
spreadsheet_id = "###"  # Please set the Spreadsheet ID.
sheetName = "Sheet1"  # Please set the sheet name.

client = gspread.authorize(credentials)
ss = client.open_by_key(spreadsheet_id)
ws = ss.worksheet(sheetName)
sheet_id = ws._properties['sheetId']
access_token = client.auth.token_response['access_token']
query = "select * where A='" + \
    searchValues[0] + "' and B='" + searchValues[1] + "'"
url = 'https://docs.google.com/spreadsheets/d/' + \
    spreadsheet_id + '/gviz/tq?tqx=out:csv&gid=' + \
    str(sheet_id) + '&tq=' + urllib.parse.quote(query)
res = requests.get(url, headers={'Authorization': 'Bearer ' + access_token})
ar = [row for row in csv.reader(io.StringIO(res.text), delimiter=',')]
print(ar)
  • 在这种情况下,当找到搜索值时,ar 具有搜索到的行。未找到搜索值时,ar 的长度为0
  • 在这种情况下,无法检索行索引。

模式 2:

在此模式中,首先从工作表中检索所有值,然后搜索这些值。

示例脚本:

searchValues = ["192.168.0.1", "Gi0/0.100"]  # Please set the search values.
spreadsheet_id = "###"  # Please set the Spreadsheet ID.
sheetName = "Sheet1"  # Please set the sheet name.

client = gspread.authorize(credentials)
ss = client.open_by_key(spreadsheet_id)
ws = ss.worksheet(sheetName)
values = ws.get_all_values()
ar = [{"rowIndex": i, "value": e} for i, e in enumerate(
    values) if e[0] == searchValues[0] and e[1] == searchValues[1]]
print(ar)
  • 在这种情况下,当找到搜索值时,ar 具有行索引和搜索行的值。未找到搜索值时,ar 的长度为0
  • 在这种情况下,可以检索行索引。

参考资料:

【讨论】:

  • 太棒了,感谢您抽出宝贵时间回答。这正是我想要的。我必须获取工作表的 ID 才能使用您的示例。为什么我在您的示例中需要 ID,但在我的代码中我从来没有 ID? scope = ['https://spreadsheets.google.com/feeds,https://www.googleapis.com/auth/drive'] 'creds = ServiceAccountCredentials.from_json_keyfile_name('xxxxxxxx.json', scope)` client = gspread.authorize(creds) sheet = client.open("Test").sheet1
  • @tars01 感谢您的回复。我很高兴你的问题得到了解决。关于你的新问题,不幸的是,我无法理解。这是因为我的英语水平不好。对此我深表歉意。
  • 感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-15
  • 2016-09-02
  • 1970-01-01
相关资源
最近更新 更多