“ if”和“ else”被忽略

时间:2020-02-14 19:22:32

标签: 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 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)

光标的输出为:

CS.D.BITCOIN.TODAY.IP 2019-05-01 00:00:00
KA.D.MXUSLN.DAILY.IP 2020-02-14 14:26:00

上面的代码输出以下内容:

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

注意-例如,史诗“ 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

为什么不考虑我的ifelse

1 个答案:

答案 0 :(得分:1)

可以考虑,但是无论如何您仍然继续遍历其他史诗并打印它们。您可以使用next代替内部的for循环,如果找到匹配项,则将其从史诗列表中删除。然后可以按要求处理所有剩余的史诗

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)

注意:这会产生一个问题,其中您的fmt网址将包含“无”,如果没有匹配项,则不确定您希望如何处理。