Python &引用;如果';及;否则";被忽视

Python &引用;如果';及;否则";被忽视,python,Python,根据标题,下面我的if/else未被考虑-不确定原因 这是我的密码: cursor.execute("SELECT epic, MAX(timestamp) FROM market_data GROUP BY epic") epics=( "KA.D.MXUSLN.DAILY.IP", "CS.D.BITCOIN.TODAY.IP", "CS.D.CRYPTOB10.TODAY.IP") for row in cursor: for epic in epics: #

根据标题,下面我的
if
/
else
未被考虑-不确定原因

这是我的密码:

cursor.execute("SELECT epic, MAX(timestamp) FROM market_data GROUP BY epic")

epics=(
"KA.D.MXUSLN.DAILY.IP",
"CS.D.BITCOIN.TODAY.IP",
"CS.D.CRYPTOB10.TODAY.IP")

for row in cursor:
    for epic in epics:
        # If epic exists in the market_data table then take the max timestamp and request new data with date1=maxtimestamp+1min and date2=now()
        if epic in row['epic']:
            date1 = row['max'] + datetime.timedelta(minutes=1)
            date2 = datetime.datetime.now()
        else:
            # if epic not already in market_data table then fresh new request with date1=now() and date2=now()+1min
            date1 = datetime.datetime.now()
            date2 = datetime.datetime.now() + datetime.timedelta(minutes=1)

            # URL PRODUCTION/LIVE Enviroment - demo most likely throttled and limited
        fmt = "https://example.com/" + str(epic) + "/1/MINUTE/batch/start/{date1:%Y/%m/%d/%H/%M/0/0}/end/{date2:%Y/%m/%d/%H/%M/%S/0}?format=json"
        # while date1 <= date2:
        url = fmt.format(epic, date1=date1, date2=date2)
        resp = requests.get(url, headers=headers)
        print(url)
上面的代码输出:

https://example.com/CS.D.BITCOIN.TODAY.IP/start/2019/05/01/00/01/0/0/end/2020/02/14/15/10/44/0?format=json
https://example.com/CS.D.CRYPTOB10.TODAY.IP/start/2020/02/14/15/10/0/0/end/2020/02/14/15/11/44/0?format=json
https://example/KA.D.MXUSLN.DAILY.IP/start/2020/02/14/14/27/0/0/end/2020/02/14/15/10/44/0?format=json
https://example.com/CS.D.BITCOIN.TODAY.IP/start/2020/02/14/15/10/0/0/end/2020/02/14/15/11/44/0?format=json
https://example.com/CS.D.CRYPTOB10.TODAY.IP/start/2020/02/14/15/10/0/0/end/2020/02/14/15/11/44/0?format=json
注意-由于epics“KA.D.MXUSLN.DAILY.IP”和“CS.D.BITCOIN.TODAY.IP”已经在游标中,我希望输出结果是:

https://example.com/CS.D.BITCOIN.TODAY.IP/start/2019/05/01/00/01/0/0/end/2020/02/14/15/10/44/0?format=json
https://example.com/CS.D.CRYPTOB10.TODAY.IP/start/2020/02/14/15/10/0/0/end/2020/02/14/15/11/44/0?format=json
https://example/KA.D.MXUSLN.DAILY.IP/start/2020/02/14/14/27/0/0/end/2020/02/14/15/10/44/0?format=json

为什么我的
if
else
没有被考虑呢?

它被考虑了,但是你还是继续迭代其他史诗并打印它们。你可以使用
next
而不是你的内部for循环,如果你找到了匹配项,就把它从史诗列表中删除。然后,任何剩余的史诗都可以在根据需要提供rds

for row in cursor:
    epic = next(epic for epic in epics if epic in row["epic"])

    if epic is not None:
        date1 = row['max'] + datetime.timedelta(minutes=1)
        date2 = datetime.datetime.now()
        epics.remove(epic)
    else:
        date1 = datetime.datetime.now()
        date2 = datetime.datetime.now() + datetime.timedelta(minutes=1)

    # URL PRODUCTION/LIVE Enviroment - demo most likely throttled and limited
    fmt = "https://example.com/" + str(epic) + "/1/MINUTE/batch/start/{date1:%Y/%m/%d/%H/%M/0/0}/end/{date2:%Y/%m/%d/%H/%M/%S/0}?format=json"
    # while date1 <= date2:
    url = fmt.format(epic, date1=date1, date2=date2)
    resp = requests.get(url, headers=headers)
    print(url)
对于光标中的行:
epic=下一个(epic为epics中的epic,如果epic在第行[“epic”])
如果epic不是无:
date1=行['max']+datetime.timedelta(分钟=1)
date2=datetime.datetime.now()
史诗。移除(史诗)
其他:
date1=datetime.datetime.now()
date2=datetime.datetime.now()+datetime.timedelta(分钟=1)
#URL生产/现场环境-演示很可能受到限制
fmt=”https://example.com/“+str(epic)+”/1/MINUTE/batch/start/{date1:%Y/%m/%d/%H/%m/0/0}/end/{date2:%Y/%m/%d/%H/%m/%S/0}?格式=json”

#date1感谢Sayse,我明白你的意思。我已经按照你的建议做了,但现在我只看到游标中存在的史诗,而不是从我的史诗列表中。@Aharon-我已经更新了我的答案,你可以跟踪你使用过的史诗。我不确定这个答案是否更好,但它看起来更接近我现在得到的匹配:AttributeError:“tuple”object没有属性“remove”,与您的approach@Aharon-啊,糟糕,没有注意到它是一个元组,把它改成一个列表,而不是我该怎么做?