For 循环和 If 语句未按预期执行

时间:2021-07-14 02:13:25

标签: regex datetime for-loop if-statement

代码如下:

# Scrape table data
alltable = driver.find_elements_by_id("song-table")

date = date.today()
simple_year_list = []
complex_year_list = []
dateformat1 = re.compile(r"\d\d\d\d")
dateformat2 = re.compile(r"\d\d\d\d-\d\d-\d\d")
for term in alltable:
    simple_year = dateformat1.findall(term.text)
    for year in simple_year:
        if 1800 < int(year) < date.year: # Year can't be above what the current year is or below 1800,
            simple_year_list.append(simple_year)     # Might have to be changed if you have a song from before 1800
        else:
            continue
    complex_year = dateformat2.findall(term.text)
    complex_year_list.append(complex_year)

代码使用正则表达式查找连续的四位数字。由于有多个 4 位数字,我想将其缩小到 1800 和 2021 之间,因为这是一个合理的时间范围。但是 simple_year_list 会打印出不符合条件的数字。

2 个答案:

答案 0 :(得分:0)

您可以在正则表达式中完成所有操作。

添加开始 ^ 和结束 $ 锚点,并通过模式限制范围:

dateformat1 = re.compile(r"^(1[89]\d\d|20([01]\d|2[01]))$")

答案 1 :(得分:0)

您没有在此处保存正确的值:

simple_year_list.append(simple_year) 

您应该保存year

simple_year_list.append(year) 

不过,我需要更多信息来进一步提供帮助。也许给我们一个您正在处理的数据样本,以及您看到的输出?