将输入值与 CSV 值匹配以发送电子邮件

Matching an input value to a CSV value for sending out emails

我尝试在网上和 Whosebug 上搜索了很长时间,但似乎找不到我需要的答案。

我正在构建一个 Python 程序,用户输入一个 ID,然后尝试将其与 CSV 文件中的一行匹配,如果成功,将从匹配的数据库中发送某些详细信息该 ID(电子邮件地址存储在 CSV 文件中)。在这个阶段,我无法将所有信息保存在一个地方(例如数据库),因为我无法控制该设置。

这是要求用户输入和 'gets' 从数据库准备的信息并启动电子邮件安全连接的代码:

cursor=conn.cursor()
retailer_id = int(input("Enter Retailer ID: "))
cursor.execute("""SELECT Retailer, Retailer_Name, Account_ID, Password from Retailers
where Retailer = ? """, (retailer_id))

users = cursor.fetchone()
try:
    users.Account_ID == retailer_id
    print("ID exists!")
    print(users.Account_ID)
except Exception as e3:
    print(e3)
    print("No retailers with this ID exists!")
    raise
print(users.Retailer, users.Retailer_Name, users.Account_ID, users.Password)

# Create a secure connection with Gmail’s SMTP server, using the SMTP_SSL() of smtplib to
initiate a TLS-encrypted connection
port = 465 # For SSL
smtp_server = "smtp.gmail.com"
sender_email = "example@example.com"
print("Would you like to send emails out to contacts?")
password = input("Type your password and press enter: ")

下面是读取 CSV 并包含一些错误处理的代码:

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 Retailer, Retailer_Name, Parent_Retailer, Email in reader:
             valid = True
             if not Retailer.strip():
                 missing.append("Retailer " + Retailer + " has Retailer ID missing!")
                 valid = False
             if not Retailer_Name.strip():
                 missing.append("Retailer " + Retailer + " has Retailer name missing!")
                 valid = False
             if not Parent_Retailer.strip():
                 missing.append("Retailer " + Retailer + " has Parent name missing!")
                 valid = False
             if not Email.strip():
                 missing.append("Retailer " + Retailer + " has Email missing!")
                 valid = False
             if Retailer is not retailer_id:
                 missing.append("IDs don't match!")
                 valid = False
             if valid:
                 server.sendmail(
                         sender_email,
                         Email,
                         message.as_string().format(
                             Retailer=Retailer,
                             Retailer_Name=Retailer_Name,
                             Parent_Retailer=Parent_Retailer,
                             Email=Email,
                             previous_month=previous_month,
                             year=year,
                             retailer_id=retailer_id),
                  )
          print(missing)
    print("Emails sent!")
except SMTPException as e2:
     print(e2)
except Exception as e:
     print("Emails not sent!")
     print(e)

示例 CSV 文件是:

Retailer Retailer_Name Parent_Retailer Email
ID 1 Retailer Name 1 1400 example1@example.com
ID 2 Retailer Name 2 1400 example2@example.com
ID 3 Retailer Name 3 1400 example3@example.com
ID 4 Retailer Name 4 1400 example4@example.com
ID 5 Retailer Name 5 1400 example5@example.com
ID 6 Retailer Name 6 1400 example6@example.com

我遇到的问题是,当我输入一个 ID 并且如果该 ID 通过所有 'error handling',那么电子邮件将发送给 CSV 中的所有 6 个人。

这些 ID 在 CSV 文件中是唯一的,但我想我只是忽略了一些非常简单的东西。

我已经尝试在块中使用以下 'error handling' 语句,但似乎没有被采纳:

if Retailer is not retailer_id:

if Retailer != retailer_id:

真的希望我已经尽可能多地解释了!

零售商是 int 类型吗?

您似乎在比较 stringint,后者总是 return 和 False -> 根本不触发。

解决方案是将字符串转换为 int int(Retailer) 或使用 QUOTE_NONNUMERIC 引用,如以下评论中的文档中所述。