gpt4 book ai didi

python - 我怎么能只比较 dd/mm 和 dd/mm/yyyy 在 Google 表格中?

转载 作者:行者123 更新时间:2023-12-05 05:35:39 24 4
gpt4 key购买 nike

今天我有一个更复杂的问题:

我有一个 Google 表格,其中一列有真实日期 ( dd/mm/yyyy)。

在检查时,我想对照以下内容进行检查:

date_format = datetime.today().strftime('%d/%m')

这只会检查 dd/mm因为我对年份不感兴趣,但也无法更改它。

Python/gspread 能解决这个问题吗?这样的比较是否可能?

在读出专栏时,当然不能给'Cell'下标。

我尝试使用我发现的一些正则表达式:

criteria_re = re.compile('(?<!\d)((((0?\d)|([12]\d)|(3[01]))/(((0?((1[02]?)|([3578]))))|(1[0-2])))|(((0?\d)|([12]\d))/(0?2))|(((0?\d)|([12]\d)|(30))/((0?[2469])|(11))))/((19)|(20))\d\d')
cell_list = worksheet.findall(criteria_re, in_column=1)

# This is returned: [<Cell R1C1 '21/08/2020'>, <Cell R2C1 '23/08/2020'>]

但是它只检查真实日期,但我只想检查条目是否与 dd/mm 匹配今天 通过使用 date_format以上。

我还尝试删除最后一个 5字符,但这会将输出转换为 str我需要它作为 int然后检查这样的事情:

cell_list = worksheet.findall(date_format, in_column=1) # All values that match the date, only want to look for dd/mm though

for match in cell_list:

values_list = worksheet.row_values(match.row) # Gets the row where the cell was found

换句话说:我仍然需要获取单元格的位置,因为我不能只搜索 dd/mm如果工作表中的格式是 dd/mm/yyyy

最佳答案

我相信你的目标如下。

  • 您想通过从 dd/mm/yyyy 的值中搜索 dd/mm 来检索行。
  • 你想使用 python 的 gspreads 来实现这一点。

在这种情况下,下面的示例脚本怎么样?在此示例脚本中,根据您的问题,我使用单元格值作为字符串值。

示例脚本:

searchColumn = 1  # Please set the search column. In this case, it's the column "A".
search = "22/08" # Please set the search value.

criteria_re = re.compile("(?<!\d)((((0?\d)|([12]\d)|(3[01]))/(((0?((1[02]?)|([3578]))))|(1[0-2])))|(((0?\d)|([12]\d))/(0?2))|(((0?\d)|([12]\d)|(30))/((0?[2469])|(11))))/((19)|(20))\d\d") # This is from your script.
values = worksheet.get_all_values()
res = [r for r in values if criteria_re.search(r[searchColumn - 1]) and r[searchColumn - 1][:-5] == search]
print(res)
  • 运行此脚本时,将从“A”列搜索值。找到搜索值后,将检索行值。

添加:

当你想检索搜索到的行号时,下面的示例脚本怎么样?在这种情况下,第 1 行的数字是 1

示例脚本:

searchColumn = 1  # Please set the search column. In this case, it's the column "A".
search = "22/08" # Please set the search value.

criteria_re = re.compile("(?<!\d)((((0?\d)|([12]\d)|(3[01]))/(((0?((1[02]?)|([3578]))))|(1[0-2])))|(((0?\d)|([12]\d))/(0?2))|(((0?\d)|([12]\d)|(30))/((0?[2469])|(11))))/((19)|(20))\d\d") # This is from your script.
values = worksheet.get_all_values()
res = [i + 1 for i, r in enumerate(values) if criteria_re.search(r[searchColumn - 1]) and r[searchColumn - 1][:-5] == search]
print(res)

关于python - 我怎么能只比较 dd/mm 和 dd/mm/yyyy 在 Google 表格中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73452510/

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