簡體   English   中英

“if”和“else”被忽略

[英]"if' and "else" being ignored

根據標題,我的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

它被考慮過,但是你仍然繼續迭代其他史詩並打印它們。 您可以使用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 url 將包含 None,如果沒有匹配項,則不確定您希望如何處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM