从 'if' 语句向空列表添加数据

Adding data to an empty list from 'if' statements

我正在使用 Python 开展一个项目,我通过该项目向 CSV 文件中的联系人列表批量发送电子邮件。

我可以正常发送电子邮件,但如果 CSV 文件中有一些缺失值,我希望能够添加一些错误处理。

我的 CSV 文件仅包含:record_id、零售商、first_name、last_name 和电子邮件。

我希望能够使用 'if' 语句捕获这些 'errors' 并将它们添加到空列表中。

然后我将使用填充的列表来存储缺失值的信息,以便我可以使用该列表在代码的其他地方显示错误(仍然不确定我要在哪里显示错误).

我遇到的问题是我无法将 'if' 语句中的值添加到空列表中。它只打印空列表。

这是我的代码示例,其中电子邮件发送部分是:

context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
    try:
        server.login(sender_email, password)
    except SMTPAuthenticationError:
        print("Username and/or password you entered is incorrect")
    try:
        with open("contacts.csv") as file: # Sending Multiple Personalized Emails using a CSV file
            reader = csv.reader(file)
            next(reader)  # Skip header row
            missing = []
            for record_id, retailer, first_name, last_name, email in reader:
                if not retailer:
                    missing.append("Record ID " + record_id + " has Retailer name missing!")
                    continue
                if not first_name:
                    missing.append("Record ID " + record_id + " has First name missing!")
                    continue
                if not last_name:
                    missing.append("Record ID " + record_id + " has Last name missing!")
                    continue
                if not email:
                    missing.append("Record ID " + record_id + " has Email missing!")
                    continue
                print(missing)
                server.sendmail(
                    sender_email,
                    email,
                    message.as_string().format(
                        record_id=record_id,
                        retailer=retailer,
                        first_name=first_name,
                        last_name=last_name,
                        email=email,
                        previous_month=previous_month,
                        year=year),
                )
        print("Emails sent!")
    except Exception as e:
        print("Emails not sent!")
        print(e)
    except SMTPException as e2:
        print(e2)

这是示例 CSV 文件:

record_id,retailer,first_name,last_name,email 
1,Store 1,Bob,Doe,example@example.com 
2,Store 2,Jane,Lang,example@example.com 
3,Store 3,Bill,Rowe,example@example.com 
4,Store 4,Rachel,Greene,, (missing email error test) 
5,Store 5,,Geller,example@example.com, (missing first name error test) 
6,,Joey,Tribiani,example@example.com (missing retailer error test)

continue 语句跳过循环体的所有其余部分并转到下一次迭代。如果第一个 if not retailer: 条件成功,您将不会进行任何其他测试。

与其在每个 if 块中执行 continue,不如设置一个变量来指示该记录是否有效。这使您可以检查所有字段。然后在所有检查之后,测试这个变量,看看你是否应该发送电子邮件。

print(missing) 应该从循环中取出。只有在所有验证都成功时才打印它。在最后执行以获取所有错误。

不相关的问题:try 中的 except: 块是按顺序测试的,所以你应该先有更具体的异常类型。 except Exception: 应该是最后一个块。

context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
    try:
        server.login(sender_email, password)
    except SMTPAuthenticationError:
        print("Username and/or password you entered is incorrect")
    try:
        with open("contacts.csv") as file: # Sending Multiple Personalized Emails using a CSV file
            reader = csv.reader(file)
            next(reader)  # Skip header row
            missing = []
            for record_id, retailer, first_name, last_name, email in reader:
                valid = True
                if not retailer:
                    missing.append("Testing")
                    valid = False
                if not retailer:
                    missing.append("Record ID " + record_id + " has Retailer name missing!")
                if not first_name:
                    missing.append("Record ID " + record_id + " has First name missing!")
                    valid = False
                if not last_name:
                    missing.append("Record ID " + record_id + " has Last name missing!")
                    valid = False
                if not email:
                    missing.append("Record ID " + record_id + " has Email missing!")
                    valid = False
                if valid:
                    server.sendmail(
                        sender_email,
                        email,
                        message.as_string().format(
                            record_id=record_id,
                            retailer=retailer,
                            first_name=first_name,
                            last_name=last_name,
                            email=email,
                            previous_month=previous_month,
                            year=year),
                    )
            print(missing)
        print("Emails sent!")
    except SMTPException as e2:
        print(e2)
    except Exception as e:
        print("Emails not sent!")
        print(e)