简体   繁体   English

如何在处理API错误时中断循环,以免丢失所有数据

[英]How to break for loop while handling API error so you don't lose all your data

I have a for loop that is continually passing a parameter to an API. 我有一个for循环,不断将参数传递给API。 I am taking the responses from the API call and appending them to 3 lists. 我将从API调用中获取响应,并将其附加到3个列表中。 Once the for loop is completed, I am creating a pandas dataframe which I am then write to a .csv file. for循环完成后,我将创建一个熊猫数据框,然后将其写入.csv文件。

The entire loop takes like 20 min to run and I just got an error 10 min in, so I lost all the data before it was able to write to a dataframe and then .csv file. 整个循环大约需要20分钟才能运行,而我在10分钟内就遇到了一个错误,因此我丢失了所有数据,然后才能将其写入数据框,然后再写入.csv文件。

How would you handle this situation better? 您如何更好地处理这种情况?

# Create empty lists for data
email = []
is_disposable_address = []
mailbox_verification = []

for address in emails_to_validate:

    parameters = {"address": address}
    response = requests.get(url, params=parameters,
                auth=("api", "key"))

    email.append(address)
    is_disposable_address.append(response.json()["is_disposable_address"])
    mailbox_verification.append(response.json()["mailbox_verification"])

# Combine lists into dictionary and create dataframe
data = {"email_address": email,
        "is_disposable_address": is_disposable_address,
        "mailbox_verification": mailbox_verification}

dF = pd.DataFrame(data)

# Export to csv
dF.to_csv("emails.csv", index=False)

The simplest solution is to just wrap the loop in a try/catch block like so: 最简单的解决方案是将循环包装在try / catch块中,如下所示:

try:
    for address in emails_to_validate:

        parameters = {"address": address}
        response = requests.get(url, params=parameters,
            auth=("api", "key"))

        email.append(address)
        is_disposable_address.append(response.json()["is_disposable_address"])
        mailbox_verification.append(response.json()["mailbox_verification"])

except Exception as e:
    print('An exception has occurred.  Giving up on loop:')
    print(e)

Execution will then continue to the subsequent part of your code. 然后执行将继续执行代码的后续部分。

You could also put the Try / Except block inside the loop if you want to continue working on subsequent list items. 如果要继续处理后续列表项,也可以将Try / Except块放入循环中。

See the docs on exception handling for more detail on how to use try/except and do more nuanced conditionals on error type. 有关如何使用try / except以及对错误类型进行更多细微差别的条件的更多详细信息,请参阅有关异常处理的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM